HorizontalScrollView_应用举例
main.java
package com.demo.scrollview;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class main extends Activity {
/** Called when the activity is first created. */
private LinearLayout mLayout;
private HorizontalScrollView sView;
private final Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontalscroll);
// 创建一个线性布局
mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);
// 创建一个ScrollView对象
sView = (HorizontalScrollView) this.findViewById(R.id.ScrollView);
Button mBtn = (Button) this.findViewById(R.id.Button);
mBtn.setOnClickListener(mClickListener);// 添加点击事件监听
}
// Button事件监听,当点击第一个按钮时增加一个button和一个textview
private Button.OnClickListener mClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
TextView tView = new TextView(main.this);// 定义一个TextView
tView.setText("ScrollView也是一个Layout布局,可以让它内部的数据显示不下的时候出现垂直滚动条,要注意的是不能在ScrollView中放多个组件");// 设置TextView的文本信息
// 设置线性布局的属性
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.FILL_PARENT
);
mLayout.addView(tView, params);// 添加一个TextView控件
mHandler.post(mScrollToButton);//传递一个消息进行滚动
}
};
private Runnable mScrollToButton = new Runnable() {
@Override
public void run() {
int off = mLayout.getMeasuredWidth() - sView.getWidth();
if (off > 0) {
sView.scrollTo( off,0);// 改变滚动条的位置
}
}
};
}
HorizontalScrollView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/Button" android:text="Button0"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<HorizontalScrollView android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:scrollbars="horizontal" >
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/TestView" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:text="ScrollView也是一个Layout布局,可以让它内部的数据显示不下的时候出现垂直滚动条,要注意的是不能在ScrollView中放多个组件" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
main.java稍作修改可以使用如下两个xml
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/TestView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestView0" />
<Button
android:id="@+id/Button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button0" >
</Button>
</LinearLayout>
</ScrollView>
mian1.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/Button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button0" >
</Button>
<ScrollView
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/TestView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestView0" />
</LinearLayout>
</ScrollView>
</LinearLayout>