繁体   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