Weight change does not take effect in layout

I think I am missing something basic here. See if anyone of you can help.

I have an activity (activity_story1) where I load some text and some other controls. The way I laid out all the components is to put them in RelativeLayout groups in the LinearLayout, and then I assigned weights to these RelativeLayouts. Sample xml below for reference.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Story1Activity" 
android:id="@+id/StoryLinearLayout">

<TextView
        android:id="@+id/titleStory"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="0.05"/>

<TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="0.90"/>

<LinearLayout
        android:id="@+id/footerStoryLinearLayout"
        android:layout_weight="0.05"
        android:layout_width="fill_parent"
        android:layout_height="0dip">
        <RelativeLayout
            android:id="@+id/footerStoryRelativeLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/PageCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="page count"/>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentTop="true"
                android:onClick="addBookmark"
                android:clickable="true"/>

        </RelativeLayout>

</LinearLayout>

      

I am loading some text in the textviews above in onCreate. So far so good.

Now I am overriding onConfigurationChanged (), since when the user changes the screen alignment, my content stays the same and onCreate () is not called to reload the content in the text views. In onConfigurationChanged (), I also use the code below to change the weight of my relativelayout "titleStoryRelativeLayout".

//change the weight of the textview when screen alignment changes
    LayoutInflater inflater = LayoutInflater.from(this);
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.activity_story1, null);
   LinearLayout yourRL = (LinearLayout)ll.findViewById(R.id.footerStoryLinearLayout);
   yourRL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f));

      

However, as soon as I rebuild my screen (when onConfigurationChanged () is called) the weight change does not take effect. I can see that the variables yourRL, etc. Are filled in. The main reason I change the weight of my Relativelayout (which contains the titleStory TextView) is because when the alignment changes, I want the height of my titleStory text to change (increase / decrease) for a better view. With the code I added to onConfigurationChanged () above, I expected the weight to change (from 0.05 to 0.01), and hence the height of the text should have changed as well.

+3


source to share


2 answers


Updated my xml to use LinearLayout (question updated with new xml) and then use below code to update Weight. I guess I'm complicating it by setting too many layouts.



LinearLayout yourRL = (LinearLayout)findViewById(R.id.footerStoryLinearLayout);
    yourRL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.5f));

      

+2


source


Because the xml file does not give an error, it does not mean that you can use these attributes ( weight

, orientation

and in your case weight

related to LinearLayout) with RelativeLayout. Check out the documentation for the RelativeLayout first . In this case, you will not find the property android:layout_weight

where, as in LinearLayout , you can find this property in the android documentation.

However, if you want to use weight, you must use a LinearLayout in a RelativeLayout with a fill_parrent height and width.



I think you understand me.

0


source







All Articles