我无法让我的按钮显示在我的 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;
}
}
}