簡體   English   中英

我如何設置膨脹的視圖寬度,它的一半是父視圖?

[英]How can i set inflated view width, half of it's parent?

我用這種方法膨脹視圖:

public TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tabs_layout, parent, false);
    return new TabViewHolder(view);
}

由於“ False ”,我的LinearLayout沒有附加到它的父級,因此沒有父級,我不能使用Layer_Weight (我用view.getParent()測試。它返回空值)。 因為當我將Layer_Weight與“Layer_Width = 0dip”結合使用時, LinearLayout消失了。

如果我使用“ True ”作為第三個參數( view.getParent()返回父級),我會收到此錯誤:

"指定的孩子已經有一個父級。你必須先在孩子的父級上調用 removeView()。 "。

這是家長:

<android.support.v7.widget.RecyclerView android:id="@+id/tabBar" style="@style/tabBar"/>

這是父樣式:

<style name="tabBar">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">0dip</item>
     <item name="android:layout_weight">0.1</item>
     <item name="android:weightSum">1</item>
     <item name="android:background">@color/headerBg</item>
</style>

這是 LinearLayout,子樣式:

<style name="tabLayout">
     <item name="android:layout_width">0dip</item>
     <item name="android:layout_height">match_parent</item>
     <item name="android:layout_weight">0.5</item>
     <item name="android:orientation">horizontal</item>
     <item name="android:gravity">center_vertical</item>
 </style>

這是孫子,TextView:

<style name="tabItem">
     <item name="android:layout_width">wrap_content</item>
     <item name="android:layout_height">wrap_content</item>
</style>

如何設置 LinearLayout 寬度,它是父級的一半?

提前欣賞。

我用這種方式來實現目標。

我將父級 recyclerView 作為參數發送給 tabBarAdapter,它膨脹了 linearLayout,子級,並在此適配器中,在 onCreateViewHolder 方法中將 linearLayout 的寬度設置為 recyclerView 的一半寬度。

view.getLayoutParams().width = tabBar.getWidth()/2;

我認為,這是反對“松散耦合”(我想將樣式與代碼完全分開)。

暫無
暫無

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

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