简体   繁体   中英

Aligning A Relative Layout Progmatically in Another Activity

Ok, I am completely stuck on this. I have looked everywhere for an answer but no solutions I found did me any good. What I am trying to do is add a relative layout to a linear layout and I can do that just fine, the problem is that I can only set the height and the width of the relative layout, and can't align it below any views in the linear layout. I'm guessing this is because the relative layout isn't a child of the linear layout? Only reason I am taking the above approach is because I need to add a relative layout for every record I have in a database table. Anyway here is the code below any help would be great :)

individual_past_test.xml file

    <?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 (relevant parts)

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

}

Ok I ended up fixing it by adding a relative layout to my xml file for past tests(which I didnt add to the question), then changed my PastTests.java as follows -

        // 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);

Here are the relevant parts to my past_tests.xml file for reference -

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

Hope this helps anyone else who is having trouble pragmatically setting views in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM