I inflate the view in this method:
public TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tabs_layout, parent, false);
return new TabViewHolder(view);
}
Because of " False ", my LinearLayout doesn't attach to it's parent therefore there is no parent and i can't use Layer_Weight (I tested with view.getParent() . it returns null). Because when i use Layer_Weight in combination with "Layer_Width = 0dip", LinearLayout disappears.
If i use " True " as third parameter( view.getParent() returns the parent), i get this error:
" The specified child already has a parent. You must call removeView() on the child's parent first. ".
This is the parent:
<android.support.v7.widget.RecyclerView android:id="@+id/tabBar" style="@style/tabBar"/>
And this is the parent style:
<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>
And this is LinearLayout, the child style:
<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>
And this is the grandchild, TextView:
<style name="tabItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
How can i set the LinearLayout width, half of it's parent?
Appreciate it in advance.
I used this way to achieve the goal.
I sent the recyclerView, the parent, as a parameter to the tabBarAdapter which inflates the linearLayout, the child, and in this adapter, set linearLayout's width to half width of recyclerView in the onCreateViewHolder method.
view.getLayoutParams().width = tabBar.getWidth()/2;
I think, this is against " Loose Coupling " (I wanted to separate styles from code completely).
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.