【发布时间】:2021-01-28 15:46:56
【问题描述】:
为此,我正在自己学习 Android,我想动态地将文本视图添加到具有相同权重的线性布局中,例如:
textview1 textview2(以此类推)
但是如果只有一个文本视图,它应该填充父级,它应该是匹配的父级,如果有两个项目,那么它们应该为每个项目填充一半的父级!
所以这是我在回收站视图中使用文本视图动态填充线性布局的填充方法:
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
这是我在回收站视图布局中的线性布局:
<LinearLayout
android:weightSum="1"
android:background="@color/colorPrimary"
android:id="@+id/resourceItemsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/subscribeButton"
android:layout_below="@+id/resourcesTitle"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_20sdp"
android:orientation="horizontal" />
这是我的类来填充框架布局内的视图:
public class PackagesViewItem extends FrameLayout {
private LinearLayout resourcesViewItemLayout;
private TextView resourcesItem;
@SuppressLint("InflateParams")
public PackagesViewItem(@NonNull Context context) {
super(context);
resourcesViewItemLayout = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.layout_packages_view_item, null);
resourcesItem = (TextView) resourcesViewItemLayout.findViewById(R.id.resourcesItem);
this.addView(resourcesViewItemLayout);
}//constructor ends
public LinearLayout getResourcesViewItemLayout() {
return resourcesViewItemLayout;
}
public void setResourcesViewItemLayout(LinearLayout resourcesViewItemLayout) {
this.resourcesViewItemLayout = resourcesViewItemLayout;
}
public TextView getResourcesItem() {
return resourcesItem;
}
public void setResourcesItem(TextView resourcesItem) {
this.resourcesItem = resourcesItem;
}
}//class 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>
问题在于,当项目是单个 textview 时,它没有填充父项,请参见此处:
我想实现这样的盒子:
【问题讨论】:
标签: java android android-layout android-linearlayout android-framelayout