Android中view和viewGroup的区别

view和viewGroup的区别
View只能操作自己

viewGroup能操作自己也可以操作孩子(就是抱在布局中的控件)

代码:

public class MainActivity extends Activity implements View.OnClickListener{
    private LinearLayout linearLayout1;
    private Button btn1;
    private ImageView iv1;
    private ImageView iv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();

    }
    /**
     * 初始化控件
     */
    private void findViews() {
        linearLayout1 = (LinearLayout)findViewById( R.id.linearLayout1 );
        btn1 = (Button)findViewById( R.id.btn1 );
        iv1 = (ImageView)findViewById( R.id.iv1 );
        iv2 = (ImageView)findViewById( R.id.iv2 );

        btn1.setOnClickListener( this );
        iv1.setOnClickListener( this );
        iv2.setOnClickListener( this );
    }
    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        if ( v == btn1 ) {
            show1(linearLayout1);
            show2(linearLayout1);
        }
    }
    /**
     * View只能操作自己
     */
    public  void show1(View view) {
        Log.e("111","linearLayout1>>>"+view.getId());
    }
    /**
     * ViewGroup可以操作孩子
     */
    public  void show2(ViewGroup viewGroup) {
        for(int i = 0; i <viewGroup.getChildCount() ; i++) {
            Log.e("111","拿到布局中的孩子"+viewGroup.getChildAt(i).getId());
        }
    }
}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.mytest.MainActivity"
    android:orientation="vertical"
    android:id="@+id/linearLayout1"
    android:background="#FF66FF"
    >
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮"
        />
    <ImageView
        android:id="@+id/iv1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"
        />
    <ImageView
        android:id="@+id/iv2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"
        />


</LinearLayout>
源码下载:

Myjstojava ---- mytest

http://download.csdn.net/download/zhaihaohao1/10020298




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值