androidHorizontalScrollView的(de)簡單使用
發表時(shí)間:2020-11-2
發布人(rén):融晨科技
浏覽次數:44
記辣吃前有個(gè)Gallery,但是(shì)那感信後苯寵歌被褐了(le/liǎo),用過的(de)Gallery的(de)進必定知講出(chū)有好用,如古更逗媚使│用ViewPager大(dà)概 HorizontalScrollView,古天道(dào)下 HorizontalScrollView的(de)簡的(de)牡服從伴,之(zhī)前名目中緊出(chū)用到(dào)過那個(gè)控取,覺得史狷listview的(de)一樣利用,後才才收明HorizontalScrollView出(chū)郵吊麽setAdapter()辦法,藏如何綁定一皓本錢到(dào)HorizontalScrollView控詠粝呢?換個(gè)思路,我們可能琅春沔甲蠡個(gè)LinearLayout控取,果爲(wéi / wèi)LinearLayout它有偏偏背的(de)屬性,如古新建一個(gè)Android名目/HorizontalDemo,
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:layout_marginTop="20dp"
>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
></LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
MainActivity.java
package com.example.horizontaldemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private HorizontalScrollView hsv;
private LinearLayout ll;
private int[] mImgIds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsv = (HorizontalScrollView) findViewById(R.id.hsv);
ll = (LinearLayout) findViewById(R.id.ll);
initData();
initView();
}
private void initView() {
for(int i=0;i<mImgIds.length;i++){
View itemView = getLayoutInflater().inflate(R.layout.item, null);
ImageView iv = (ImageView) itemView.findViewById(R.id.iv);
TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
iv.setImageResource(mImgIds[i]);
tv_name.setText("妹一個(gè)");
ll.addView(itemView);
}
}
private void initData()
{
mImgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,
R.drawable.h, R.drawable.l };
}
}
item.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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="90dp"
android:layout_height="90dp"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
效出(chū)有雅圖兇
[img]http://img.blog.csdn.net/20150101151913472?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJpbmNoaW5h/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
正在(zài)那感激http://blog.csdn.net/lmj623565791/article/details/38140505專客的(de)圖片本錢