android控制滚动条滚动
android控制滾動條滾動
運行結果:
主Activity:
public class MainActivity extends Activity {
private RelativeLayout topbar;
private RelativeLayout scroll;
private LayoutInflater inflater;
private LinearLayout topbarLinea;
private LinearLayout scrollLinea;
private Button lastButton;
private Button nextButton;
View v1;
HorizontalScrollView v2;
LinearLayout v3;
@Override
protected void 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();
}
public void addLastListener(){
lastButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v2.smoothScrollTo(v2.getScrollX() - 100, 0);
}
});
}
public void addNextListener(){
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v2.smoothScrollTo(v2.getScrollX() + 100, 0);
}
});
}
}
主布局文件:<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=".MainActivity" >
? ? <LinearLayout android:id="@+id/topbar_linea"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content">
?? ? ? ?
? ? </LinearLayout>
?? ?
? ? <LinearLayout android: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>
?? ?
? ? <RelativeLayout android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:layout_below="@id/scroll_linea">
?? ? ? ?
? ? ? ? <Button android: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"/>
? ? ? ? <Button android: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>
滾動條布局文件:
<RelativeLayout xmlns: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">
?? ? ? ?
?? ? ? ? <LinearLayout android:id="@+id/content_linea"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content">
?? ? ? ?
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京1"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京2"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京3"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京4"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京5"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京6"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京7"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京8"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="北京9"
? ? ? ? ? ? android:textSize="18sp"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:layout_marginRight="10dp"/>
? ? ? ? <TextView android: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布局文件:
<RelativeLayout xmlns: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
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的android控制滚动条滚动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 使用xml定义自己的Vi
- 下一篇: Ubuntu下配置samba实现文件夹共