按钮未显示在自定义列表视图实现中

时间:2013-09-13 15:37:24

标签: java android xml listview

我无法将我的按钮显示在我的ListView中,除了按钮外,其他所有内容都显示得很好。

我的自定义行文件:custom_list_view_row.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="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/color_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textIsSelectable="false"/>
        <TextView
            android:id="@+id/color_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:textSize="15sp"
            android:textStyle="italic"
            android:textIsSelectable="false"/>
    </LinearLayout>
    <Button
        android:id="@+id/btnColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        android:background="#ff005500"/>

</LinearLayout>

这是我的主要活动:activity_color_selector:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="26sp"
        android:text="@string/choose_color"
        android:textColor="@android:color/holo_blue_bright"/>
    <ListView
        android:id="@android:id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

</LinearLayout>

这是扩展ListActivity的活动类:

public class ColorSelector extends ListActivity {

    private final String[] items = {
        "Color #1",
        "Color #2",
        "Color #3",
        "Color #4",
        "Color #5",
        "Color #6"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_selector);
        setListAdapter(new CustomListViewAdapter());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.color_selector, menu);
        return true;
    }

    class CustomListViewAdapter extends ArrayAdapter<String>{

        public CustomListViewAdapter(){
            super(
                ColorSelector.this,
                R.layout.custom_list_view_row,
                R.id.color_number,
                items);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            // Call the super class' getView method
            View row = super.getView(position, convertView, parent);

            // Get all of the widgets in the row template
            TextView colorNumber = (TextView)row.findViewById(R.id.color_number);
            TextView colorName = (TextView)row.findViewById(R.id.color_name);
            Button colorButton = (Button)row.findViewById(R.id.btnColor);
            String[] colorArray = getResources().getStringArray(R.array.color_array);

            // Set the widgets appropriate values
            colorNumber.setText("Color #" + (position + 1));
            colorName.setText(colorArray[position]);
            colorButton.setVisibility(View.VISIBLE);
            colorButton.setBackgroundColor(Color.parseColor(colorArray[position]));

            return row;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

代码中按钮上方的线性布局将其宽度设置为match_parent,因此填充屏幕的宽度并为您的按钮留下空间:

    <LinearLayout
    android:layout_width="match_parent"

您需要将此更改为wrap_content,或使用布局权重,或切换到relativeLayout来解决此问题。如果该按钮应位于包含文本视图的linear_layout下,请将父级linearLayout的方向切换为垂直

答案 1 :(得分:0)

由于layout_width,您的按钮隐藏在视图中。在开始第二个linear_layout之前添加按钮。但这肯定会改变你的设计。

这是带按钮的布局:           

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="155dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/color_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="dfasd"
        android:textIsSelectable="false"/>
    <TextView
        android:id="@+id/color_name"
        android:text="dfas11111d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:textSize="15sp"
        android:textStyle="italic"
        android:textIsSelectable="false"/>


</LinearLayout>