当前位置: 编程技术>移动开发
本页文章导读:
▪java内部类详解及运用 java内部类详解及应用
今天在公司上班闲着无聊于是就写了下java内部类。
里面包含了关于内部类的介绍及详解,最后还有一个内部类的简单应用。堪称经典。
贴出来晒下希望能帮到大家。
.........
▪ strlen与sizeof的差别 strlen与sizeof的区别
strlen与sizeof的区别最后一个int a[3] = {1, 2, 3}; 在32位机上,sizeof a 应该等于4×3 = 12吧。 sizeof 运算符是用来求内存容量字节的大小的。而strlen是用来求字符串实际长度的。.........
▪ 让GridView拥有Gallery的拖动效能 让GridView拥有Gallery的拖动功能
利用Gallery的拖动功能,能很容易的将在一行上显示不下的内容显示出来。这种特性可以用在菜单上(如果菜单足够多,以至一行显示不下)。但是Gallery有个.........
[1]java内部类详解及运用
来源: 互联网 发布时间: 2014-02-18
java内部类详解及应用
今天在公司上班闲着无聊于是就写了下java内部类。
里面包含了关于内部类的介绍及详解,最后还有一个内部类的简单应用。堪称经典。
贴出来晒下希望能帮到大家。
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
/**
* java内部类
*
* 内部类是指在一个外部类的内部在定义一个类
* 内部类作为外部类的一个成员,并且依附外部类而存在。
* 内部类可以为静态,可用protected和private修饰(而外部类不可以,外部类只能用public和default)。
* 分类:成员内部类、局部内部类、静态内部类、匿名内部类。
* @author Administrator
*
*/
public class Outer {
/**
* 成员内部类 代码
*/
private static int i=1;
private int j=10;
private int k=20;
public static void outer_f1(){
}
public void outer_f2(){
}
/**
* 外部类的非静态方法访问成员内部类
*/
public void outer_f3(){
Inner inner=new Inner();
inner.inner_f1();
}
/**
* 外部类的静态方法访问成员内部类
*/
public static void outer_f4(){
//step1 建立外部类对象
Outer outer=new Outer();
//step2 根据外部类对象建立内部类对象
Inner inner=outer.new Inner();
//step3 访问内部类方法
inner.inner_f1();
}
/**
* 成员内部类
* 作为外部类的一个成员存在,与外部类的属性、方法并列。
* 优点:
* 1、内部类作为外部类的成员,可以访问外部类的私有成员或属性。(即使声明为private,但是对于处于其内部的内部类还是可见的。)
* 2、可以内部类定义在外部类不可访问的属性。这样就在外部类中实现了比外部类private还要小的额访问权限。
* 注意:
* 1、内部类是一个编译时的概念,一旦编译成功,就会成为完全不同的两个类。
* 对于一个名为Outer的外部类和其内部定义的名为Inner的内部类。编译完成后出现Outer.class 和 Outer$Inner.class 两个类
* 2、当Outer是一个private类时,外部类对于其外部访问是私有的,所以就无法建立外部类对象,进而也无法建立内部类对象。
* @author Administrator
*
*/
class Inner{
//static int inner_i=100; 内部类中不允许定义静态变量,内部类作为外部类的一个成员,成员内部不允许定义静态变量.
int j=100;//内部类和外部类的实例变量可以共存
private int inner_i=1;
void inner_f1(){
System.out.println(inner_i);
System.out.println(j);//在内部类中访问内部类自己的变量直接使用变量名
System.out.println(this.j);//或者使用this.变量名
System.out.println(Outer.this.j);//在内部类中访问外部类中与内部类同名的实例变量 用外部类名.this.变量名
System.out.println(k);//如果内部类中没有与外部类同名的变量,则可以直接用变量名访问外部类变量
outer_f1();
outer_f2();
}
}
/**
* 局部内部类代码
*/
private int s=100;
private int out_i=1;
public void outer_f5(final int k){
final int s=200;
int i=1;
final int j=10;
/**
* 局部内部类
* 在方法中第一的内部类称为局部内部类。
* 与局部变量类似,在局部内部类前不加修饰符public和private,其范围为定义它的代码块
* 注意:
* 1、在类外不可直接生产局部内部类(保证局部内部类对外是不可见的)。
* 2、要想使用局部内部类时需要生产对象,对象调用方法,在方法中才能调用局部内部类。
* 3、通过内部类和接口达到一个强制的弱耦合,用局部内部类来实现接口,并在方法中返回接口类型,使局部内部类不可见,屏蔽实现类的可见性。
* @author Administrator
*
*/
class Inner{
int s=300;//可以定义与外部类同名的变量
//static int m=20;不可以定义静态变量
/**
* 内部类构造函数
*/
Inner(int k){
inner_f(k);
}
int inner_i=100;
/**
* 内部类的方法
* @param k
*/
void inner_f(int k){
System.out.println(out_i);//如果内部类没有与外部类同名的变量,在内部类中可以直接访问外部类的实例变量
System.out.println(j);//可以访问外部内的局部变量(即方法内的变量,但是变量必须是final)
System.out.println(s);//如果内部类中有与外部类同名的变量,直接用变量名访问的是内部类的变量
System.out.println(this.s);//用this.变量名 访问的也是内部类变量
System.out.println(Outer.this.s);///用外部类名.this.内部类变量名 访问的是外部类变量
}
}
new Inner(k);
}
/**
* 静态内部类代码
* 注意:
* 前三种内部类与变量类似,所以可以对照参考变量
*/
private static int a=1;
private int b=10;
public static void outer_f6(){
}
public void outer_f7(){
}
/**
* 静态内部类
* 静态内部类可以使用public,protected,private修饰
* 静态内部类中可以定义静态和非静态的成员
* 注意:
* 一个静态内部类不需要一个外部类的成员:只是静态内部类和成员内部类的区别。静态内部类的对象可以直接生成
* Outer.Inner2 in=new Outer.Inner2();
* 这实际上静态内部类成为了一个顶级类。
* 静态内部类不可用private来进行定义。
* 当类与接口(或者是接口与接口)发生方法命名冲突的时候,此时必须使用内部类来实现。用接口不能完全地实现多继承,用接口配合内部类才能实现真正的多继承。
* 例子:class People{ run(); } interface Machine{ run(); } class Robot extends People implement Machine
* @author Administrator
*
*/
static class Inner2{
static int inner_i=100;
int inner_j=200;
static void inner_f1(){
System.out.println("Outer.a:"+a); //静态内部类只能访问外部类的静态成员
outer_f6();//包括静态变量和静态方法
}
void inner_f2(){
//System.out.println("Outer.b:"+b); 静态内部类不能访问外部类的非静态成员
//outer_f7(); X 包括非静态变量和非静态方法
}
}
/**
* 外部类访问静态内部类
*/
public void outer_f8(){
//外部内访问内部类的静态成员:内部类.静态成员
System.out.println(Inner2.inner_i);
Inner2.inner_f1();
//外部类访问内部类非静态成员:实例化内部类
Inner2 inner=new Inner2();
System.out.println(inner.inner_j);
inner.inner_f2();
}
/**
* 匿名内部类
*
* 匿名内部类就是没有名字的内部类。
* 注意:
* 1、匿名内部类不能有构造函数
* 2、匿名内部类不能定义任何静态成员、方法和类
* 3、匿名内部类不能是public、protected、private、static
* 4、只能创建匿名内部类的一个实例
* 5、一个匿名内部类一定是在new后面,用其隐含实现一个接口或实现一个类。
* 6、因匿名内部类为局部内部类,所以局部内部类的所有限制都对其生效
* @return
*/
public Contents outer_f9(){
return new Contents (){
private int i=10;
@Override
public int getValue() {
// TODO Auto-generated method stub
return i;
}
};//在匿名内部类末尾的分号,并不是用来标记此内部类结束(C++中是那样)。实际上,它标记的是表达式的结束,只不过这个表达式正巧包含了内部类罢了。因此,这与别的地方使用的分号是一致的。
//等同于
// class MyContents implements Contents {
// private int i=10;
// @Override
// public int getValue() {
// // TODO Auto-generated method stub
// return i;
// }
//
// }
// return new MyContents();
}
public Wrapping outer_f10( int x){
final int z=x;
return new Wrapping(x){
public void dest(){
System.out.println("outer_f10: "+z);//可以访问外部内的局部变量(即方法内的变量,但是变量必须是final)
}
};
}
public static void main(String[] args){
outer_f4();//成员内部类
new Outer().outer_f5(4);//局部内部类
new Outer().outer_f8();//静态内部类
new Outer().outer_f9().getValue();//匿名内部类
new Outer().outer_f10(8).dest() ;//匿名内部类
InnerClassTest obj=new InnerClassTest();
obj.launchFrame();
}
}
/**
* 一个内部类的简单应用
*
* @author Administrator
*
*/
class InnerClassTest{
private Frame f;
private TextField tf;
public InnerClassTest(){
f=new Frame("Inner classes example");
tf=new TextField(30);
}
public void launchFrame(){
Label label=new Label("Click and drag the mouse");
f.add(label,BorderLayout.NORTH);
f.add(tf,BorderLayout.SOUTH);
//成员内部类
f.addMouseMotionListener(new MyMouseMotionListener());/*参数为内部类对象*/
//等效 匿名内部类
// f.addMouseMotionListener(new MouseMotionAdapter(){
// public void mouseDragged(MouseEvent e) {
// String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
// tf.setText(s);
// }
//
// });
f.setSize(300,200);
f.setVisible(true);
}
class MyMouseMotionListener extends MouseMotionAdapter{ /*内部类开始*/
public void mouseDragged(MouseEvent e) {
String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
tf.setText(s); }
}
//内部类结束
}
interface Contents{
int getValue();
}
class Wrapping{
int j;
Wrapping(int i){
j=i;
}
public void dest(){
System.out.println("Wrapping:"+j);
}
}
参考资源:http://blog.zol.com.cn/863/article_862638.html
[2] strlen与sizeof的差别
来源: 互联网 发布时间: 2014-02-18
strlen与sizeof的区别
strlen与sizeof的区别
最后一个int a[3] = {1, 2, 3};
在32位机上,sizeof a 应该等于4×3 = 12吧。
sizeof 运算符是用来求内存容量字节的大小的。而strlen是用来求字符串实际长度的。如果int *p = a; sizeof(p)应该等于4咯。
1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型。
该类型保证能容纳实现所建立的最大对象的字节大小。
2.sizeof是算符,strlen是函数。
3.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以''\0''结尾的。
4.数组做sizeof的参数不退化,传递给strlen就退化为指针了。
5.大部分编译程序在编译的时候就把sizeof计算过了 是类型或是变量的长度这就是sizeof(x)可以用来定义数组维数的原因
char str[20]="0123456789";//str是编译期大小已经固定的数组
int a=strlen(str); //a=10;//strlen()在运行起确定
int b=sizeof(str); //而b=20;//sizeof()在编译期确定
6.strlen的结果要在运行的时候才能计算出来,是用来计算字符串的实际长度,不是类型占内存的大小。
7.sizeof后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为sizeof是个操作符不是个函数。
char c;
sizeof c;//变量名可以不加括弧
8.当适用了于一个结构类型时或变量, sizeof 返回实际的大小,
当适用一静态地空间数组, sizeof 归还全部数组的尺寸。
sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸
9.数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址,
如:
fun(char [8])
fun(char [])
都等价于 fun(char *)
在C++里参数传递数组永远都是传递指向数组首元素的指针,编译器不知道数组的大小
如果想在函数内知道数组的大小, 需要这样做:
进入函数后用memcpy拷贝出来,长度由另一个形参传进去
fun(unsiged char *p1, int len)
{
unsigned char* buf = new unsigned char[len+1]
memcpy(buf, p1, len);
}
我们能常在用到 sizeof 和 strlen 的时候,通常是计算字符串数组的长度
看了上面的详细解释,发现两者的使用还是有区别的,从这个例子可以看得很清楚:
har str[11]="0123456789";//注意这里str大小因该大于等于11,应考虑'\0'在内,否则编译器会报错
int a=strlen(str); //a=10; >>>> strlen 计算字符串的长度,以结束符 0x00 为字符串结束。
int b=sizeof(str); //而b=11; >>>> sizeof 计算的则是分配的数组 str[11] 所占的内存空间的大小,不受里面存储的内容改变。
上面是对静态数组处理的结果,如果是对指针,结果就不一样了
char* ss = "0123456789";
sizeof(ss) 结果 4 ===》ss是指向字符串常量的字符指针,sizeof 获得的是一个指针的之所占的空间,应该是长整型的,所以是4
sizeof(*ss) 结果 1 ===》*ss是第一个字符 其实就是获得了字符串的第一位'0' 所占的内存空间,是char类型的,占了 1 位strlen(ss)= 10 >>>> 如果要获得这个字符串的长度,则一定要使用 strlen
另外,下面的方法可以用于确定该静态数组可以容纳元素的个数:
int a[3]={1,2,3};
cout << sizeof a/sizeof ( typeid( a[0] ).name() );
strlen与sizeof的区别
最后一个int a[3] = {1, 2, 3};
在32位机上,sizeof a 应该等于4×3 = 12吧。
sizeof 运算符是用来求内存容量字节的大小的。而strlen是用来求字符串实际长度的。如果int *p = a; sizeof(p)应该等于4咯。
1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型。
该类型保证能容纳实现所建立的最大对象的字节大小。
2.sizeof是算符,strlen是函数。
3.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以''\0''结尾的。
4.数组做sizeof的参数不退化,传递给strlen就退化为指针了。
5.大部分编译程序在编译的时候就把sizeof计算过了 是类型或是变量的长度这就是sizeof(x)可以用来定义数组维数的原因
char str[20]="0123456789";//str是编译期大小已经固定的数组
int a=strlen(str); //a=10;//strlen()在运行起确定
int b=sizeof(str); //而b=20;//sizeof()在编译期确定
6.strlen的结果要在运行的时候才能计算出来,是用来计算字符串的实际长度,不是类型占内存的大小。
7.sizeof后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为sizeof是个操作符不是个函数。
char c;
sizeof c;//变量名可以不加括弧
8.当适用了于一个结构类型时或变量, sizeof 返回实际的大小,
当适用一静态地空间数组, sizeof 归还全部数组的尺寸。
sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸
9.数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址,
如:
fun(char [8])
fun(char [])
都等价于 fun(char *)
在C++里参数传递数组永远都是传递指向数组首元素的指针,编译器不知道数组的大小
如果想在函数内知道数组的大小, 需要这样做:
进入函数后用memcpy拷贝出来,长度由另一个形参传进去
fun(unsiged char *p1, int len)
{
unsigned char* buf = new unsigned char[len+1]
memcpy(buf, p1, len);
}
我们能常在用到 sizeof 和 strlen 的时候,通常是计算字符串数组的长度
看了上面的详细解释,发现两者的使用还是有区别的,从这个例子可以看得很清楚:
har str[11]="0123456789";//注意这里str大小因该大于等于11,应考虑'\0'在内,否则编译器会报错
int a=strlen(str); //a=10; >>>> strlen 计算字符串的长度,以结束符 0x00 为字符串结束。
int b=sizeof(str); //而b=11; >>>> sizeof 计算的则是分配的数组 str[11] 所占的内存空间的大小,不受里面存储的内容改变。
上面是对静态数组处理的结果,如果是对指针,结果就不一样了
char* ss = "0123456789";
sizeof(ss) 结果 4 ===》ss是指向字符串常量的字符指针,sizeof 获得的是一个指针的之所占的空间,应该是长整型的,所以是4
sizeof(*ss) 结果 1 ===》*ss是第一个字符 其实就是获得了字符串的第一位'0' 所占的内存空间,是char类型的,占了 1 位strlen(ss)= 10 >>>> 如果要获得这个字符串的长度,则一定要使用 strlen
另外,下面的方法可以用于确定该静态数组可以容纳元素的个数:
int a[3]={1,2,3};
cout << sizeof a/sizeof ( typeid( a[0] ).name() );
[3] 让GridView拥有Gallery的拖动效能
来源: 互联网 发布时间: 2014-02-18
让GridView拥有Gallery的拖动功能
利用Gallery的拖动功能,能很容易的将在一行上显示不下的内容显示出来。
这种特性可以用在菜单上(如果菜单足够多,以至一行显示不下)。
但是Gallery有个不爽的地方,就是被点击的那个item会一直出现在中间,有时候我们并不需要这样的“智能”!怎么办呢?
那就利用GridView,将GridView放在HorizontalScrollView中,如下:
Xml代码 收藏代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent">
6.
7. <RelativeLayout android:background="#030e13"
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. >
11. <ImageView android:id="@+id/webnav_left"
12. android:layout_width="8dip"
13. android:layout_height="wrap_content"
14. android:layout_centerVertical="true"
15. android:src="/blog_article/@drawable/news_left/index.html"
16. />
17. <ImageView android:id="@+id/webnav_right"
18. android:layout_width="8dip"
19. android:layout_height="wrap_content"
20. android:layout_alignParentRight="true"
21. android:layout_centerVertical="true"
22. android:src="/blog_article/@drawable/news_right/index.html"
23. />
24. <HorizontalScrollView android:layout_width="fill_parent"
25. android:layout_height="wrap_content"
26. android:layout_toLeftOf="@id/webnav_right"
27. android:layout_toRightOf="@id/webnav_left"
28. android:scrollbars="none">
29. <LinearLayout android:layout_width="fill_parent"
30. android:layout_height="wrap_content">
31. <LinearLayout android:id="@+id/layout_webnav"
32. android:layout_width="800dip"
33. android:layout_height="wrap_content"
34. android:orientation="horizontal">
35. <GridView android:id="@+id/gallery_webnav"
36. android:layout_width="fill_parent"
37. android:layout_height="fill_parent"
38. android:background="#030e13"
39. android:gravity="center"
40. android:numColumns="auto_fit"
41. android:listSelector="#00000000">
42. </GridView>
43. </LinearLayout>
44. </LinearLayout>
45. </HorizontalScrollView>
46. </RelativeLayout>
47.
48. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/webnav_left"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="/blog_article/@drawable/news_left/index.html"
/>
<ImageView android:id="@+id/webnav_right"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="/blog_article/@drawable/news_right/index.html"
/>
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/webnav_right"
android:layout_toRightOf="@id/webnav_left"
android:scrollbars="none">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/layout_webnav"
android:layout_width="800dip"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView android:id="@+id/gallery_webnav"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#030e13"
android:gravity="center"
android:numColumns="auto_fit"
android:listSelector="#00000000">
</GridView>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</LinearLayout>
注意上面id为layout_webnav的LinearLayout,这里的layout_width是需要计算的!见下面代码。
Java代码 收藏代码
1. public class Test_2_Activity extends Activity{
2.
3. private final int per=3;//每行显示的个数
4. private GridView gridView;
5. @Override
6. public void onCreate(Bundle savedInstanceState) {
7. super.onCreate(savedInstanceState);
8. setContentView(R.layout.test2);
9. DisplayMetrics dm = new DisplayMetrics();
10. dm = getApplicationContext().getResources().getDisplayMetrics();
11. int menuWidth = dm.widthPixels-16;
12.
13. gridView= (GridView) findViewById(R.id.gallery_webnav);
14. int itemWidth = menuWidth/per;
15. gridView.setColumnWidth(itemWidth);
16.
17. ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
18. Map<String,String> map;
19. for(int i=0;i<5;i++){
20. map=new HashMap<String,String>();
21. map.put("simple_item_1", "name"+i);
22. map.put("simple_item_2", "age"+i);
23. map.put("simple_item_3", "class"+i);
24. data.add(map);
25. }
26. int resource=R.layout.row_test2;
27. String[] from={"simple_item_1","simple_item_2","simple_item_3"};
28. int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
29. SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
30.
31. gridView.setAdapter(adapter);
32.
33. //让GridView一行显示,这里的layout_width是需要计算的
34. LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav);
35. layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
36.
37. gridView.setSelection(0);
38. gridView.setOnItemClickListener(listener);
39. }
40. }
public class Test_2_Activity extends Activity{
private final int per=3;//每行显示的个数
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
int menuWidth = dm.widthPixels-16;
gridView= (GridView) findViewById(R.id.gallery_webnav);
int itemWidth = menuWidth/per;
gridView.setColumnWidth(itemWidth);
ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
Map<String,String> map;
for(int i=0;i<5;i++){
map=new HashMap<String,String>();
map.put("simple_item_1", "name"+i);
map.put("simple_item_2", "age"+i);
map.put("simple_item_3", "class"+i);
data.add(map);
}
int resource=R.layout.row_test2;
String[] from={"simple_item_1","simple_item_2","simple_item_3"};
int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
gridView.setAdapter(adapter);
//让GridView一行显示,这里的layout_width是需要计算的
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav);
layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
gridView.setSelection(0);
gridView.setOnItemClickListener(listener);
}
}
注意上面的itemWidth*data.size(),这里才是LinearLayout的实际宽度!
R.layout.row_test2布局如下:
Xml代码 收藏代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView android:id="@+id/simple_item_1"
8. android:layout_width="fill_parent"
9. android:layout_height="fill_parent"
10. android:gravity="center"
11. />
12. <RelativeLayout android:background="#030e13"
13. android:layout_width="fill_parent"
14. android:layout_height="fill_parent"
15. >
16. <TextView android:id="@+id/simple_item_2"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:layout_alignParentLeft="true"
20. android:paddingLeft="10dp"
21. />
22. <TextView android:id="@+id/simple_item_3"
23. android:layout_width="wrap_content"
24. android:layout_height="wrap_content"
25. android:layout_alignParentRight="true"
26. android:paddingRight="10dp"
27. />
28. </RelativeLayout>
29. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
/>
<TextView android:id="@+id/simple_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
/>
</RelativeLayout>
</LinearLayout>
运行后显示的效果:
看不出什么,呵呵,拖动一下看看。
gundumw100
利用Gallery的拖动功能,能很容易的将在一行上显示不下的内容显示出来。
这种特性可以用在菜单上(如果菜单足够多,以至一行显示不下)。
但是Gallery有个不爽的地方,就是被点击的那个item会一直出现在中间,有时候我们并不需要这样的“智能”!怎么办呢?
那就利用GridView,将GridView放在HorizontalScrollView中,如下:
Xml代码 收藏代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent">
6.
7. <RelativeLayout android:background="#030e13"
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. >
11. <ImageView android:id="@+id/webnav_left"
12. android:layout_width="8dip"
13. android:layout_height="wrap_content"
14. android:layout_centerVertical="true"
15. android:src="/blog_article/@drawable/news_left/index.html"
16. />
17. <ImageView android:id="@+id/webnav_right"
18. android:layout_width="8dip"
19. android:layout_height="wrap_content"
20. android:layout_alignParentRight="true"
21. android:layout_centerVertical="true"
22. android:src="/blog_article/@drawable/news_right/index.html"
23. />
24. <HorizontalScrollView android:layout_width="fill_parent"
25. android:layout_height="wrap_content"
26. android:layout_toLeftOf="@id/webnav_right"
27. android:layout_toRightOf="@id/webnav_left"
28. android:scrollbars="none">
29. <LinearLayout android:layout_width="fill_parent"
30. android:layout_height="wrap_content">
31. <LinearLayout android:id="@+id/layout_webnav"
32. android:layout_width="800dip"
33. android:layout_height="wrap_content"
34. android:orientation="horizontal">
35. <GridView android:id="@+id/gallery_webnav"
36. android:layout_width="fill_parent"
37. android:layout_height="fill_parent"
38. android:background="#030e13"
39. android:gravity="center"
40. android:numColumns="auto_fit"
41. android:listSelector="#00000000">
42. </GridView>
43. </LinearLayout>
44. </LinearLayout>
45. </HorizontalScrollView>
46. </RelativeLayout>
47.
48. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/webnav_left"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="/blog_article/@drawable/news_left/index.html"
/>
<ImageView android:id="@+id/webnav_right"
android:layout_width="8dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="/blog_article/@drawable/news_right/index.html"
/>
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/webnav_right"
android:layout_toRightOf="@id/webnav_left"
android:scrollbars="none">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/layout_webnav"
android:layout_width="800dip"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView android:id="@+id/gallery_webnav"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#030e13"
android:gravity="center"
android:numColumns="auto_fit"
android:listSelector="#00000000">
</GridView>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</LinearLayout>
注意上面id为layout_webnav的LinearLayout,这里的layout_width是需要计算的!见下面代码。
Java代码 收藏代码
1. public class Test_2_Activity extends Activity{
2.
3. private final int per=3;//每行显示的个数
4. private GridView gridView;
5. @Override
6. public void onCreate(Bundle savedInstanceState) {
7. super.onCreate(savedInstanceState);
8. setContentView(R.layout.test2);
9. DisplayMetrics dm = new DisplayMetrics();
10. dm = getApplicationContext().getResources().getDisplayMetrics();
11. int menuWidth = dm.widthPixels-16;
12.
13. gridView= (GridView) findViewById(R.id.gallery_webnav);
14. int itemWidth = menuWidth/per;
15. gridView.setColumnWidth(itemWidth);
16.
17. ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
18. Map<String,String> map;
19. for(int i=0;i<5;i++){
20. map=new HashMap<String,String>();
21. map.put("simple_item_1", "name"+i);
22. map.put("simple_item_2", "age"+i);
23. map.put("simple_item_3", "class"+i);
24. data.add(map);
25. }
26. int resource=R.layout.row_test2;
27. String[] from={"simple_item_1","simple_item_2","simple_item_3"};
28. int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
29. SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
30.
31. gridView.setAdapter(adapter);
32.
33. //让GridView一行显示,这里的layout_width是需要计算的
34. LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav);
35. layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
36.
37. gridView.setSelection(0);
38. gridView.setOnItemClickListener(listener);
39. }
40. }
public class Test_2_Activity extends Activity{
private final int per=3;//每行显示的个数
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
int menuWidth = dm.widthPixels-16;
gridView= (GridView) findViewById(R.id.gallery_webnav);
int itemWidth = menuWidth/per;
gridView.setColumnWidth(itemWidth);
ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
Map<String,String> map;
for(int i=0;i<5;i++){
map=new HashMap<String,String>();
map.put("simple_item_1", "name"+i);
map.put("simple_item_2", "age"+i);
map.put("simple_item_3", "class"+i);
data.add(map);
}
int resource=R.layout.row_test2;
String[] from={"simple_item_1","simple_item_2","simple_item_3"};
int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
gridView.setAdapter(adapter);
//让GridView一行显示,这里的layout_width是需要计算的
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav);
layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
gridView.setSelection(0);
gridView.setOnItemClickListener(listener);
}
}
注意上面的itemWidth*data.size(),这里才是LinearLayout的实际宽度!
R.layout.row_test2布局如下:
Xml代码 收藏代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView android:id="@+id/simple_item_1"
8. android:layout_width="fill_parent"
9. android:layout_height="fill_parent"
10. android:gravity="center"
11. />
12. <RelativeLayout android:background="#030e13"
13. android:layout_width="fill_parent"
14. android:layout_height="fill_parent"
15. >
16. <TextView android:id="@+id/simple_item_2"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:layout_alignParentLeft="true"
20. android:paddingLeft="10dp"
21. />
22. <TextView android:id="@+id/simple_item_3"
23. android:layout_width="wrap_content"
24. android:layout_height="wrap_content"
25. android:layout_alignParentRight="true"
26. android:paddingRight="10dp"
27. />
28. </RelativeLayout>
29. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
<RelativeLayout android:background="#030e13"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/simple_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
/>
<TextView android:id="@+id/simple_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
/>
</RelativeLayout>
</LinearLayout>
运行后显示的效果:
看不出什么,呵呵,拖动一下看看。
gundumw100
最新技术文章: