簡體   English   中英

如何以編程方式在線性布局內設置卡片視圖的布局寬度?

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

我正在嘗試在MATCH_PARENT和0dp之間更改兩個cardview元素的布局寬度。 當我通過xml更改模擬場景時,它是正確的,但是,當我以編程方式進行操作時,它並沒有按預期方式呈現。

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>      

爪哇

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

非常感謝。

弄清楚了。 為了達到預期效果,還必須設置邊距和布局權重。

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

暫無
暫無

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

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