标签:horizontalscrollview 水平左右滑动 linearlayout imageview 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>
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 实例讲解HorizontalScrollView实现左右滑动,布布扣,bubuko.com
Android 实例讲解HorizontalScrollView实现左右滑动
标签:horizontalscrollview 水平左右滑动 linearlayout imageview textview
原文地址:http://blog.csdn.net/a123demi/article/details/38308079