java - 按钮未显示在自定义 ListView 实现中

标签 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 :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 的 Activity 类:

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;
        }
    }
}

最佳答案

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

    <LinearLayout
    android:layout_width="match_parent"

您需要将其更改为wrap_content,或使用布局权重,或切换到relativeLayout来解决此问题。如果按钮应该位于包含 TextView 的 Linear_layout 下,请将父 linearLayout

的方向切换为垂直

关于java - 按钮未显示在自定义 ListView 实现中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18790193/

相关文章:

java - 如何将 hashmap 中的值存储在字符串数组中

android - Fragments 中的共享元素和内容转换

java - 无法在 Android 10 中创建目录

android - 我该如何着手制作这个布局?遇到这么多麻烦

java - 对于 Java 版 YouTube API V3,访问 token 始终会在 1 小时后过期

java - 在 Openshift maven RESTEasy Tomcat7 中找不到相关资源

java - 单击后退按钮时滑动加载的图像会重置

java - 具有字段名称和值的 XML 以响应无法从字段中检索的映射 Java 对象

java - 如何在Android中的tablelayout上进行快速滚动

java - 未找到 JNA。 native 方法将被禁用。 java.lang.ClassNotFoundException : com. sun.jna.Native 异常