Android HorizontalScrollView 点滴

Android HorizontalScrollView点滴

1、HorizontalScrollView是只能有一个Child。

package com.example.mytest;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
<span style="white-space:pre">		</span>//正确的添加多个Button<span style="white-space:pre">	</span>
		/*
		final LinearLayout layout2=new LinearLayout(this);//定义一个新的LinearLayout
		layout2.setOrientation(LinearLayout.HORIZONTAL);//设置为水平
		
		for(int i=0;i<6;i++) //向LinearLayout中添加按钮
		{
			Button button=new Button(this);
			button.setId(i);
			button.setText("i"+i);
			layout2.addView(button);
		}
		HorizontalScrollView horizontalscrollview=(HorizontalScrollView)findViewById(R.id.hds);
		horizontalscrollview.addView(layout2);//向水平滚动条中添加LinearLayout
		*/
		
<span style="white-space:pre">		</span>//错误的添加方法
<span style="white-space:pre">		</span>/*
		HorizontalScrollView hsv=(HorizontalScrollView)findViewById(R.id.hds);
		Button btn = new Button(this); 
		btn.setText("第一个");
		//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
		//hsv.addView(btn,params);
		 hsv.addView(btn);
		 Button btn2 = new Button(this); 
		 btn2.setText("第二个");
		 hsv.addView(btn1);
<span style="white-space:pre">		</span>*/

	}
}

2、布局文件

<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="${relativePackage}.${activityClass}" >


        
<HorizontalScrollView 
     android:id="@+id/hds"
     android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbarAlwaysDrawHorizontalTrack="false">

</HorizontalScrollView>        

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值