Android HorizontalScrollView点滴
1、HorizontalScrollView是只能有一个Child。
package com.example.mytest;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>//正确的添加多个Button<span style="white-space:pre"> </span>
/*
final LinearLayout layout2=new LinearLayout(this);//定义一个新的LinearLayout
layout2.setOrientation(LinearLayout.HORIZONTAL);//设置为水平
for(int i=0;i<6;i++) //向LinearLayout中添加按钮
{
Button button=new Button(this);
button.setId(i);
button.setText("i"+i);
layout2.addView(button);
}
HorizontalScrollView horizontalscrollview=(HorizontalScrollView)findViewById(R.id.hds);
horizontalscrollview.addView(layout2);//向水平滚动条中添加LinearLayout
*/
<span style="white-space:pre"> </span>//错误的添加方法
<span style="white-space:pre"> </span>/*
HorizontalScrollView hsv=(HorizontalScrollView)findViewById(R.id.hds);
Button btn = new Button(this);
btn.setText("第一个");
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//hsv.addView(btn,params);
hsv.addView(btn);
Button btn2 = new Button(this);
btn2.setText("第二个");
hsv.addView(btn1);
<span style="white-space:pre"> </span>*/
}
}
2、布局文件
<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"
tools:context="${relativePackage}.${activityClass}" >
<HorizontalScrollView
android:id="@+id/hds"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbarAlwaysDrawHorizontalTrack="false">
</HorizontalScrollView>
</RelativeLayout>