我想ListView
通过按钮切换 a 的可见性,但是当我@android:id/list
用于 ListView 时,我不知道如何通过以下方式在 Activity 中检索视图findViewById(R.id. list);
:
为此,我有以下代码:
public void feedButtonListener() {
final ImageButton ToggleButton = (ImageButton) findViewById(R.id.ToggleFeed);
ToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "Toggleis clicked!",
Toast.LENGTH_SHORT).show();
ListView listView = (ListView) findViewById(R.id.list); //HOW?
if (feedView) {
listView.setVisibility(View.GONE);
ToggleButton
.setImageResource(R.drawable.ic_inactive);
feedView = false;
} else {
listView.setVisibility(View.VISIBLE);
ToggleButton
.setImageResource(R.drawable.ic_active);
feedView = true;
}
}
});
在布局中:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="@dimen/margin"
android:layout_marginTop="@dimen/margin" >
</ListView>