我有一个自定义ViewGroup
,可以从ListView
布局中膨胀xml
。列表项布局从另一个xml
文件中膨胀。所有视图都设置为fill_parent
。 ListView
填充其父级,但ListView
项不填充。
我已尝试将ListView
放入LinearLayout
并为其分配权重。试过了RelativeLayout
。另外,我已经构建了ListView
programmaticaly,而没有使用xml
布局。在将视图添加到LayoutParams
之前,甚至更改了ViewGroup
。
我也考虑过这些帖子:Width of clickable area in ListView w/ onListItemClick,In Android, how can I set a ListView item's height and width?,Android Listview width prob。
为什么物品没有扩展到填充宽度的任何想法?以及如何扩展它们?
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;
}
答案 0 :(得分:1)
ViewGroup
并未最终正常运作。我也试过使用LinearLayout
。但RelativeLayout
最终正确地夸大了布局。因此,我的xml
布局和适配器保持不变,但我将主类更改为扩展RelativeLayout
。同时删除了onLayout()
。
MyView类:
public class MyView extends RelativeLayout {
//Extra code unchanged ....
private void generateMyView()
{
ListView menu = (ListView) View.inflate(getContext(), R.layout.layout_main, null);
menu.setAdapter(new MyAdapter(getContext()));
this.addView(menu);
}
//Removed onLayout()
}
答案 1 :(得分:0)
如果您覆盖onLayout
,您也应该覆盖onMeasure
..几乎总是......
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
// this.setLayoutParams(new
// *ParentLayoutType*.LayoutParams(parentWidth,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
如果仍然无效,请使用注释行。
另一个选择是忘记onLayout
和onMeasure
,并覆盖onFinishInflate()
并将这些行添加到其中。如果您从xml中充气MyViewGroup
。
protected void onFinishInflate () {
super.onFinishInflate();
ListView main = (ListView) View.inflate(getContext(),
R.layout.layout_main,null);
main.setAdapter(new MyAdapter(getContext()));
this.addView(main);
}