android如何在Expandablelistview的页脚中心创建一个按钮

时间:2013-02-12 15:26:42

标签: android android-layout android-xml

因为ExpandableListView的页脚没有内置,我自己构建了它,我在上面放了一个按钮。我试图让这个按钮位于该页脚的中心。

页脚all_addresses_group_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/b_all_addresses_group_footer_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="0dip"
        android:text="@string/b_edit" />

</LinearLayout>

上面代码中的按钮位于中心位置,但当我将其添加到ExpandableListView组时,它会保留在左侧。

以编程方式添加页脚

@Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        AllAddressChild child = (AllAddressChild) getChild(groupPosition,
                childPosition);
        if (childPosition == groups.get(groupPosition).getChilds().size() - 1) {
            // if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.all_addresses_group_footer, null);
            // }
            Button b_edit = (Button) convertView
                    .findViewById(R.id.b_all_addresses_group_footer_edit);
            b_edit.setText("Edit");
            b_edit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(context, EditAddress.class);
                    ((Activity) context).startActivityForResult(intent, 104);
                }
            });
            convertView.setPadding(0, 0, 0, 20);
            return convertView;
        } else { another code

在上面的代码中,convertView是包含页脚的视图。

TRUE if语句中添加了页脚

我做错了什么?以及如何在中心制作按钮?

由于

发表评论

enter image description here

asewr

之后
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:layout_marginTop="20dip"
        android:text="@string/tv_allAddresses"
        android:textColor="#025f7c"
        android:textSize="25dip"
        android:typeface="serif" />

    <ExpandableListView
        android:id="@+id/elv_all_addresses_addresses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:padding="10dip" >
    </ExpandableListView>

    <Button
        android:id="@+id/b_all_addresses_addAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/button_selector"
        android:paddingTop="20dip"
        android:text="@string/b_addAddress"
        android:paddingRight="5dip"
        android:paddingLeft="5dip"
        android:textColor="#FFFFFF" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

我自己找到了解决方案,在利用hierarchyviewer工具后,解决方案是将expandablelistview的宽度设为fill parententer image description here

<ExpandableListView
        android:id="@+id/elv_all_addresses_addresses"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:padding="10dip" >
    </ExpandableListView>

答案 1 :(得分:1)

您需要为视图设置LayoutParams

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
convertView.setLayoutParams(layoutParams);