简体   繁体   中英

How do you set the layout width of a cardview inside a linearlayout programmatically?

I am trying to change the layout width of two cardview element between MATCH_PARENT and 0dp. When I simulate the scenario via xml changes, it is correct, however, when I do it programmatically, it is not rendering as expected.

xml

        <LinearLayout
            android:id="@+id/lDates"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/card_margins"
            android:layout_marginEnd="@dimen/card_margins"
            android:layout_below="@id/lnrButtons"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:id="@+id/cardSectionA"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:cardElevation="0dp"
                android:layout_marginEnd="@dimen/card_margins"
                android:layout_marginBottom="@dimen/card_margins"
                app:cardCornerRadius="4dp"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/textLabelA"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/sample_value"/>

            </android.support.v7.widget.CardView> 

            <android.support.v7.widget.CardView
                android:id="@+id/cardSectionB"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:cardElevation="0dp"
                android:layout_marginEnd="@dimen/card_margins"
                android:layout_marginBottom="@dimen/card_margins"
                app:cardCornerRadius="4dp"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/textLabelB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sample_value"/>

            </android.support.v7.widget.CardView>                          
        </LinearLayout>      

java

//as-is scenario; cardSectionA and cardSectionB should be shown
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        cardSectionA.getLayoutParams().width,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

cardSectionA.setLayoutParams(params);
cardSectionB.setLayoutParams(params);   

//should only show cardSectionA scenario
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

cardSectionA.setLayoutParams(params);

Thanks a lot.

Figured it out. To behave as expected, the margin and layout weight must also be set.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

int margin = 10;
params.setMargins(margin, margin, margin, margin);
params.weight = 1;


cardSectionA.setLayoutParams(params);

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