簡體   English   中英

在另一個活動中以編程方式對齊相對布局

[英]Aligning A Relative Layout Progmatically in Another Activity

好吧,我完全堅持這一點。 我到處都在尋找答案,但沒有找到對我有任何好處的解決方案。 我想做的是在線性布局中添加相對布局,我可以做到這一點,問題是我只能設置相對布局的高度和寬度,而不能在任何視圖下對齊它在線性布局中。 我猜這是因為相對布局不是線性布局的子級嗎? 我采用上述方法的唯一原因是因為我需要為數據庫表中的每個記錄添加相對布局。 無論如何,這是下面的代碼,對您的幫助將是巨大的:)

personal_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>

IndividualPastTest.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);

}

好的,我最后通過將相對布局添加到我的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視圖困難的人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM