android控制滚动条滚动
来源:互联网 发布:葛优的网络意思是什么 编辑:程序博客网 时间:2024/03/28 21:40
android控制滚动条滚动
运行结果:
主Activity:
public class MainActivity extends Activity {
private RelativeLayouttopbar;
private RelativeLayoutscroll;
private LayoutInflaterinflater;
private LinearLayouttopbarLinea;
private LinearLayoutscrollLinea;
private ButtonlastButton;
private ButtonnextButton;
View v1;
HorizontalScrollView v2;
LinearLayout v3;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = LayoutInflater.from(this);
topbar = (RelativeLayout)inflater.inflate(R.layout.topbar,null);
scroll = (RelativeLayout)inflater.inflate(R.layout.hscroll_bar,null);
topbarLinea = (LinearLayout)findViewById(R.id.topbar_linea);
scrollLinea = (LinearLayout)findViewById(R.id.scroll_linea);
lastButton = (Button)findViewById(R.id.the_last);
nextButton = (Button)findViewById(R.id.the_next);
topbarLinea.addView(topbar);
scrollLinea.addView(scroll);
v2 = (HorizontalScrollView)scroll.findViewById(R.id.hscroll_bar);
v3 = (LinearLayout)scroll.findViewById(R.id.content_linea);
addLastListener();
addNextListener();
// WindowManager manage=getWindowManager();
// Display display=manage.getDefaultDisplay();
// screenHeight=display.getHeight();
// screenWidth=display.getWidth();
}
publicvoid addLastListener(){
lastButton.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
v2.smoothScrollTo(v2.getScrollX() - 100, 0);
}
});
}
publicvoid addNextListener(){
nextButton.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
v2.smoothScrollTo(v2.getScrollX() + 100, 0);
}
});
}
}
主布局文件:<RelativeLayoutxmlns: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=".MainActivity">
<LinearLayoutandroid:id="@+id/topbar_linea"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayoutandroid:id="@+id/scroll_linea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topbar_linea"
android:layout_marginTop="20dp">
</LinearLayout>
<RelativeLayoutandroid:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_below="@id/scroll_linea">
<Buttonandroid:id="@+id/the_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="上一个"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<Buttonandroid:id="@+id/the_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="下一个"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</RelativeLayout>
滚动条布局文件:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<HorizontalScrollView
android:id="@+id/hscroll_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayoutandroid:id="@+id/content_linea"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京1"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京2"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京3"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京4"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京5"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京6"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京7"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京8"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京9"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京10"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
topbar布局文件:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="topBar"
android:gravity="center"
android:textSize="20sp"/>
</RelativeLayout>
demo下载地址:
http://download.csdn.net/download/lyhdream/5210462
- android控制滚动条滚动
- android 通过按钮控制水平滚动条
- 滚动条定位控制
- 滚动条定位控制
- js控制滚动条
- 滚动条样式控制
- webbrowser滚动条控制
- css控制滚动条
- 滚动条控制大全
- div滚动条控制
- js滚动条控制
- 控制滚动条位置
- div 滚动条 同步 控制 div 滚动条 滚动
- 简单的Jquery控制滚动条滚动
- JS控制滚动条滚动的位置
- 模拟滚动条控制内容的滚动
- C#中鼠标滚动控制滚动条滚动
- 页面滚动条控制详细
- [MTK FP]用Python把图片资源image.rar中为.pbm后缀的文件更改为.bmp后缀的方法
- poj 2488——dfs深度优先遍历
- WinForm
- 单例(态)设计模式
- Google的社会化梦想与Reader
- android控制滚动条滚动
- Jump Game
- 从在QT中找不到QVBOX类到QT4与QT3的兼容
- Think different & be yourself 缅怀乔布斯
- Hadoop2013
- (转载) 关于MotionEvent.ACTION_UP无法被响应的问题
- 拼成最小的数
- java计算距当前时间30天前的日期和时间
- hdu 1209 Clock