【发布时间】:2014-06-05 02:58:27
【问题描述】:
我目前在设计可以从“网格”视图切换到“列表”视图的活动时遇到问题。我正在使用 GridView 并切换到“列表”,我将最大列数设置为 1。现在我的问题是,当我切换到“列表”视图时,我想要左边的图像和一些文本在右侧,而不仅仅是中间的图像。所有数据均取自手动填充的自定义类。以下图片将演示。
我想要什么:
我有什么:
我的代码(XML 和 Java)如下:
single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:contentDescription="@string/content"
android:id="@+id/imageView1"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gridview.MainActivity"
tools:ignore="MergeRootFrame" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="128dp"
android:verticalSpacing="10dp"
>
</GridView>
</FrameLayout>
MainActivity.java
private GridView myGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGrid = (GridView) findViewById(R.id.gridView1);
myGrid.setAdapter(new myGridAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
GridView gv = (GridView) findViewById(R.id.gridView1);
//ImageView iv = (ImageView) findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.action_grid:
Toast.makeText(this, "Grid Button", Toast.LENGTH_SHORT).show();
gv.setNumColumns(GridView.AUTO_FIT);
return true;
case R.id.action_list:
Toast.makeText(this, "List Button", Toast.LENGTH_SHORT).show();
gv.setNumColumns(1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
有谁知道我可以如何做到这一点?我已经阅读了很长一段时间,也想到了 viewSwitcher 和 viewFlipper,但不认为它们正是我想要的。我还考虑过为每个布局创建单独的活动的想法,但也了解了缺点。
感谢您的宝贵时间。
【问题讨论】:
标签: android list gridview textview switch-statement