利用Gallery的拖動功能,能很容易的將在一行上顯示不下的內容顯示出來。
這種特性可以用在菜單上(如果菜單足夠多,以至一行顯示不下)。
但是Gallery有個不爽的地方,就是被點擊的那個item會一直出現在中間,有時候我們并不需要這樣的“智能”!怎么辦呢?
那就利用GridView,將GridView放在HorizontalScrollView中,如下:
注意上面id為layout_webnav的LinearLayout,這里的layout_width是需要計算的!見下面代碼。
注意上面的itemWidth*data.size(),這里才是LinearLayout的實際寬度!
R.layout.row_test2布局如下:
運行后顯示的效果:
看不出什么,呵呵,拖動一下看看。
這種特性可以用在菜單上(如果菜單足夠多,以至一行顯示不下)。
但是Gallery有個不爽的地方,就是被點擊的那個item會一直出現在中間,有時候我們并不需要這樣的“智能”!怎么辦呢?
那就利用GridView,將GridView放在HorizontalScrollView中,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/webnav_left"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/news_left"
/>
<ImageView android:id="@+id/webnav_right"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/news_right"
/>
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/webnav_right"
android:layout_toRightOf="@id/webnav_left"
android:scrollbars="none">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/layout_webnav"
android:layout_width="800dip"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView android:id="@+id/gallery_webnav"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#030e13"
android:gravity="center"
android:numColumns="auto_fit"
android:listSelector="#00000000">
</GridView>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</LinearLayout>
注意上面id為layout_webnav的LinearLayout,這里的layout_width是需要計算的!見下面代碼。
public class Test_2_Activity extends Activity{
private final int per=3;//每行顯示的個數
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
int menuWidth = dm.widthPixels-16;
gridView= (GridView) findViewById(R.id.gallery_webnav);
int itemWidth = menuWidth/per;
gridView.setColumnWidth(itemWidth);
ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
Map<String,String> map;
for(int i=0;i<5;i++){
map=new HashMap<String,String>();
map.put("simple_item_1", "name"+i);
map.put("simple_item_2", "age"+i);
map.put("simple_item_3", "class"+i);
data.add(map);
}
int resource=R.layout.row_test2;
String[] from={"simple_item_1","simple_item_2","simple_item_3"};
int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
gridView.setAdapter(adapter);
//讓GridView一行顯示,這里的layout_width是需要計算的
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav);
layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
gridView.setSelection(0);
gridView.setOnItemClickListener(listener);
}
}
注意上面的itemWidth*data.size(),這里才是LinearLayout的實際寬度!
R.layout.row_test2布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
/>
<TextView android:id="@+id/simple_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
/>
</RelativeLayout>
</LinearLayout>
運行后顯示的效果:
看不出什么,呵呵,拖動一下看看。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

