我如何设置膨胀的视图宽度,它的父视图宽度的一半?
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() 测试过。它 returns 为空)。因为当我将 Layer_Weight 与 "Layer_Width = 0dip" 结合使用时,LinearLayout 消失了。
如果我使用“True”作为第三个参数(view.getParent() returns 父级) ,我得到这个错误:
"指定的子项已有父项。您必须先在子项的父项上调用 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;
我认为,这违背了“松散耦合”(我想将样式与代码完全分离)。
我用这个方法膨胀视图:
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() 测试过。它 returns 为空)。因为当我将 Layer_Weight 与 "Layer_Width = 0dip" 结合使用时,LinearLayout 消失了。
如果我使用“True”作为第三个参数(view.getParent() returns 父级) ,我得到这个错误:
"指定的子项已有父项。您必须先在子项的父项上调用 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;
我认为,这违背了“松散耦合”(我想将样式与代码完全分离)。