0

好的,我完全坚持这一点。我到处寻找答案,但没有找到任何解决方案对我有任何好处。我想要做的是将相对布局添加到线性布局中,我可以做到这一点,问题是我只能设置相对布局的高度和宽度,并且不能在任何视图下方对齐在线性布局中。我猜这是因为相对布局不是线性布局的子布局?我采用上述方法的唯一原因是因为我需要为数据库表中的每条记录添加相对布局。无论如何,这是下面的代码,任何帮助都会很棒:)

individual_past_test.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/past_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E4EFF5" >

       <TextView
         android:id="@+id/candidate_name"
         style="@style/past_test_candidate"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="25dp" />

     </RelativeLayout>

个人PastTest.java

    public class IndividualPastTests extends RelativeLayout {

    private Context mContext;

    public IndividualPastTests(Context context) {
    super(context);

    mContext = context;

    String infService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li;

    li = (LayoutInflater) getContext().getSystemService(infService);
    li.inflate(R.layout.individual_past_test, this, true);
    }
    }

PastTests.java(相关部分)

    past_tests_label = (TextView) findViewById(R.id.past_tests_label);
    past_tests_label.setId(1);

    addViewForFirstTest();

    private void addViewForFirstTest() {

    past_test = new IndividualPastTests(this);
    RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    past_test.setGravity(Gravity.CENTER_HORIZONTAL);

    // Not being called?
    past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());

            past_test_view_params.height = 100;
    past_test_view_params.width = 600;


    System.out.println(past_tests_label.getId());

    past_test.setLayoutParams(past_test_view_params);       
    this.addContentView(past_test, past_test_view_params);

}
4

1 回答 1

0

好的,我最终通过在我的 xml 文件中为过去的测试添加相对布局来修复它(我没有添加到问题中),然后按如下方式更改了我的 PastTests.java -

        // Added this line to get the RelativeLayout I created in the xml to hold my view
        pasts_tests_holder = (RelativeLayout) findViewById(R.id.past_tests_holder)

        past_tests_label = (TextView) findViewById(R.id.past_tests_label);
        past_tests_label.setId(1);

        addViewForFirstTest();

    private void addViewForFirstTest() {

        past_test = new IndividualPastTests(this);
        RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        past_test.setGravity(Gravity.CENTER_HORIZONTAL);

        past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());

        past_test_view_params.height = 100;
        past_test_view_params.width = 600;

        past_test.setLayoutParams(past_test_view_params);       

        // Changed this line to add the past_test view to my new relative layout 
        past_tests_holder.addView(past_test);

以下是我的 past_tests.xml 文件的相关部分以供参考 -

    <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/prev_test_view"
          android:orientation="vertical" >

         <RelativeLayout
             android:id="@+id/past_tests"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_below="@+id/past_tests_label"
             android:layout_centerHorizontal="true"
             android:layout_marginTop="227dp" >

         </RelativeLayout> 
      </LinearLayout>

希望这可以帮助其他在 android 中实际设置视图时遇到问题的人

于 2012-06-08T01:59:19.173 回答