Android 实例讲解HorizontalScrollView实现左右滑动
2014-07-30 22:38
726 查看
本博文主要讲解怎么使用HorizontalScrollView实现左右滑动的效果。
HorizontalScrollView实际上是一个FrameLayout ,一般通过只放置一个LinearLayout子控件。如果要使其添加其他的控件,就使用LinearLayout子控件来添加其他的控件,最后达到丰富其内容的效果。其中,LinearLayout设置的orientation布局为Horizontal.HorizontalScrollView不可以和ListView同时用,因为ListView有自己的滚动条设置。类似TextView也一样。
下面通过一个实际的实例来讲解HorizontalScrollView:
1.效果:
2.实现代码
需求分析:主要实现一个父LinearLayout中包含代码创建的10个子LinearLayout,而每个子LinearLayout添加一个ImageView和TextView,超出的部分实现左右滑动
activity_main.xml
MainActivity.java
以上为本博客所讲内容。
HorizontalScrollView实际上是一个FrameLayout ,一般通过只放置一个LinearLayout子控件。如果要使其添加其他的控件,就使用LinearLayout子控件来添加其他的控件,最后达到丰富其内容的效果。其中,LinearLayout设置的orientation布局为Horizontal.HorizontalScrollView不可以和ListView同时用,因为ListView有自己的滚动条设置。类似TextView也一样。
下面通过一个实际的实例来讲解HorizontalScrollView:
1.效果:
2.实现代码
需求分析:主要实现一个父LinearLayout中包含代码创建的10个子LinearLayout,而每个子LinearLayout添加一个ImageView和TextView,超出的部分实现左右滑动
activity_main.xml
<span style="font-family:Microsoft YaHei;font-size:18px;"><LinearLayout 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" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00FF00" android:scrollbarAlwaysDrawHorizontalTrack="false" > <LinearLayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> </LinearLayout></span>
MainActivity.java
package com.example.horizontalscrollviewdemo; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private HorizontalScrollView scrollView; private LinearLayout linear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = (HorizontalScrollView) this.findViewById(R.id.scroll_view); linear = (LinearLayout) this.findViewById(R.id.linear); createChildLinearLayout(); } private void createChildLinearLayout() { for (int i = 0; i < 10; i++) { LinearLayout.LayoutParams linearLp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LinearLayout myLinear = new LinearLayout(this); linearLp.setMargins(5, 0, 5, 20); myLinear.setOrientation(LinearLayout.VERTICAL); myLinear.setTag(i); linear.addView(myLinear, linearLp); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); ImageView imageView = new ImageView(this); imageView.setBackgroundResource(R.drawable.img); myLinear.addView(imageView, lp); LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); TextView textView = new TextView(this); textView.setText(i + ""); textView.setGravity(Gravity.CENTER); myLinear.addView(textView, textViewLp); myLinear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, v.getTag().toString(), Toast.LENGTH_SHORT).show(); } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
以上为本博客所讲内容。
相关文章推荐
- 实现android左右滑动效果:ViewFlipper、ViewPager、HorizontalScrollView
- Android 用ScrollView和HorizontalScrollView同时实现上下、左右滚动
- Android_自定义HorizontalScrollView实现滑动子控件大小伸缩
- Android HorizontalScrollView结合GridView实现类似网易新闻那样的栏目滑动效果
- Android ScrollViewy与HorizontalScrollView实现上下左右滚动
- Android HorizontalScrollView左右滑动
- Android 用HorizontalScrollView实现滑动标签tabView
- Android中抽屉实现,HorizontalScrollView横向滑动传值后获取数据
- 布局动画Android ScrollView HorizontalScrollView 实现全方向(上下左右)反弹效果
- HorizontalScrollView 嵌套listview实现列表左右、上下滑动、控件复用等功能
- Android HorizonScrollView实现图片左右滑动
- Android ScrollViewy与HorizontalScrollView实现上下左右滚动
- Android ViewPager+HorizontalScrollView实现标题栏滑动(腾讯新闻)
- HorizontalScrollView实现多页左右滑动
- HorizontalScrollView里嵌套ListView实现上下左右滑动表格
- android 使用HorizontalScrollView 实现标题带动内容左右切屏
- Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突
- Android 自定义 HorizontalScrollView 实现图片左右滚动按钮控制
- Android HorizontalScrollView左右滑动效果