Java 水平地将文本视图动态添加到等权重线性布局
我正在自己学习Android,因此我想动态地将TextView添加到线性布局中,并为expample设置相等的权重: textview1 textview2(依此类推) 但是,如果只有一个文本视图,它应该填充父视图,即它应该是匹配父视图,如果有两个项目,则它们应该为每个项目填充父视图的一半 因此,这是我在recycler视图中的填充方法,用于使用文本视图动态填充线性布局:Java 水平地将文本视图动态添加到等权重线性布局,java,android,android-layout,android-linearlayout,android-framelayout,Java,Android,Android Layout,Android Linearlayout,Android Framelayout,我正在自己学习Android,因此我想动态地将TextView添加到线性布局中,并为expample设置相等的权重: textview1 textview2(依此类推) 但是,如果只有一个文本视图,它应该填充父视图,即它应该是匹配父视图,如果有两个项目,则它们应该为每个项目填充父视图的一半 因此,这是我在recycler视图中的填充方法,用于使用文本视图动态填充线性布局: private void populateResourcesItems(ArrayList<ResourcesItem
private void populateResourcesItems(ArrayList<ResourcesItem> resources, int position, RecyclerViewHolder recyclerViewHolder) {
recyclerViewHolder.resourceItemsLayout.removeAllViews();
for (int its = 0; its < resources.size(); its++) {
PackagesViewItem packagesViewItem = new PackagesViewItem(context);
if (hasValue(resources.get(its).getResourceName())) {
packagesViewItem.getResourcesItem().setText(resources.get(its).getResourceName());
packagesViewItem.getResourcesItem().setSelected(true);
}
//add the view item layout to adapter container
recyclerViewHolder.resourceItemsLayout.addView(packagesViewItem);
}//for end
}//populateResourcesItems ends
此视图项类的布局为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/resourcesViewItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/packagesTabTextColorUnselected">
<TextView
android:id="@+id/resourcesItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/opensans_regular"
android:gravity="center"
android:includeFontPadding="false"
android:text="@string/mobile_bundles_resources"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/_12ssp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
问题是,当项目为single textview时,它没有填充父项。请参见此处:
我想实现以下目标:
删除LinearLayout
android:weightSum=“1”
并定义此LinearLayout.LayoutParams
:
val linearLayout : LinearLayout = findViewById(R.id.layout)
val lp = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT)
lp.weight = 1f
然后仅应用于布局中添加的视图:
val textView : TextView = TextView(context)
textView.setText("Text1")
textView.height = ...
//...
val textView2 : TextView = TextView(context)
textView2.setText("Text2")
textView2.height = ...
//....
linearLayout.addView(textView, lp)
linearLayout.addView(textView2, lp)
代码中有很多问题,但问题是不必要的边距,所以 不要设置它们
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_20sdp"
也可以删除android:weightSum=“1”并像Gabriele Mariotti建议的那样为创建的TextView
s设置权重
val textView : TextView = TextView(context)
textView.setText("Text1")
textView.height = ...
//...
val textView2 : TextView = TextView(context)
textView2.setText("Text2")
textView2.height = ...
//....
linearLayout.addView(textView, lp)
linearLayout.addView(textView2, lp)
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_20sdp"