设为首页 加入收藏

TOP

Android2.2以上使用HorizontalScrollView取代Gallery
2014-11-24 07:37:41 来源: 作者: 【 】 浏览:1
Tags:Android2.2 以上 使用 HorizontalScrollView 取代 Gallery

我现在需要的是在手机上显示可以滑动的几张图片,也就是各个主题对应的图片,然后用户点击图片进行切换就行了,使用Gallery需要设置相应的适配器,但是使用HorizontalScrollView却显得非常的简单。


具体代码如下:


1 创建xml:


< xml version="1.0" encoding="utf-8" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:saveEnabled="false"
android:scrollbars="none" >


android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >


android:id="@+id/theme1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/theme1" >


android:id="@+id/theme2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/theme2" >



2 控制代码


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;


public class GalleryTest extends Activity implements OnClickListener{


private ImageView theme1,theme2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.gallery);


theme1=(ImageView)findViewById(R.id.theme1);
theme2=(ImageView)findViewById(R.id.theme2);
theme1.setOnClickListener(this);
theme2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==theme1){
Toast.makeText(this, "主题1", Toast.LENGTH_SHORT).show();
//真正的主题切换
}else if(v==theme2){
Toast.makeText(this, "主题2", Toast.LENGTH_SHORT).show();
//真正的主题切换
}


}


}


这样就可以,效果图如下(仅仅演示而已):



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇iOS 6.1系统日程事件的新增代码 下一篇Android自定义Toast

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)