我有一个从布局ViewGroup
中膨胀 a的自定义。列表项布局是从另一个文件扩展而来的。所有视图都设置为。填充其父级,但项目不填充。ListView
xml
xml
fill_parent
ListView
ListView
我试过把它放进ListView
去LinearLayout
并给它分配权重。也试过RelativeLayout
了。另外,我已经以ListView
编程方式构建了该布局,而没有使用xml
布局。甚至LayoutParams
在将视图添加到ViewGroup
.
我也考虑了这些帖子:ListView w/ onListItemClick 中可点击区域的宽度,在 Android 中,如何设置 ListView 项目的高度和宽度?, Android Listview 宽度概率。
关于为什么项目不扩展到填充宽度的任何想法?以及如何扩展它们?
MyViewGroup 类:
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
generateMyViewGroup();
}
private void generateMyViewGroup()
{
ListView main = (ListView) View.inflate(getContext(), R.layout.layout_main, null);
main.setAdapter(new MyAdapter(getContext()));
this.addView(main);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
this.getChildAt(0).layout(l, t, r, b);
}
}
ListView
xml
布局:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:background="#77000000"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" >
</ListView>
ListView
项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_main_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/mainBackground"
android:gravity="fill_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/main_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:paddingBottom="7dp"
android:paddingLeft="20dp"
android:paddingRight="5dp"
android:paddingTop="20dp"
android:text="test"
android:textColor="@color/mainCategory"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
部分MyAddapter
课程:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(null == convertView)
{
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.layout_main_category, null);
viewHolder = new ViewHolder();
viewHolder.mainItemTitle = (TextView) convertView.findViewById(R.id.main_category);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mainItemTitle.setText("text");
return convertView;
}
private static class ViewHolder {
public TextView menuItemTitle;
}