安卓 控件的AttributeSet

AttributeSet在Android控件中扮演重要角色,它用于接收XML中定义的属性信息,如layout_width和layout_height。在TextView等控件的构造函数中,attrs参数用于传递这些属性值。在实际开发中,通过创建并添加View到如RelativeLayout或LinearLayout等ViewGroup子类时,可以使用AttributeSet传递定制的属性,使得新View具备特定样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在接触控件中,常常可以看到AttributeSet attrs这个参数。例:

   TextView(Context context, AttributeSet attrs, int defStyleAttr)
     举例:
   <TextView android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

   其中context是当前Activity的Context,attrs是一组属性值,例如layout_width,layout_height等等,defStyleAttr和控件的style有关。

   AttributeSet 是接收xml中定义的属性信息,这不一定是自定义布局,不是自定义布局也有该属性,要不xml中定义的属性信息就无法接收了。

其中的layout_width,layout_height,text都可以在AttributeSet 中接收到。


在实际应用中,我可以通过创建view,把view添加到继承了ViewGroup的子类中,如RelativeLayout,LinearLayout等。而再次创建一个view,直接addView把AttributeSet 参数带入,新的view就具有该属性。

public class MainActivity extends Activity {  
  
    private RelativeLayout mRelativeLayout;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mRelativeLayout=(RelativeLayout)findViewById(R.id.relativelayout1);  
        ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                XmlPullParser parser = MainActivity.this.getResources().getXml(R.layout.textview);  
                AttributeSet attributes = Xml.asAttributeSet(parser);  
                int type;  
                try{  
                    while ((type = parser.next()) != XmlPullParser.START_TAG &&  
                            type != XmlPullParser.END_DOCUMENT) {  
                        // Empty  
                    }  
      
                    if (type != XmlPullParser.START_TAG) {  
                        Log.e("","the xml file is error!\n");  
                    }     
                } catch (XmlPullParserException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                Log.d("",""+parser.getAttributeCount());  
                TextView tv=new TextView(MainActivity.this,attributes);  
                RelativeLayout.LayoutParams params=new LayoutParams(MainActivity.this,attributes);  
                mRelativeLayout.addView(tv,0,params);  
            }  
        });  
    }  
  
    @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);  
    }  
}  
 

res/layout/activity_main.xml


<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"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context="com.example.helloworld.MainActivity" >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world" />  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/textView1"  
        android:text="Button" />  
  
    <RelativeLayout  
        android:id="@+id/relativelayout1"  
        android:layout_width="match_parent"  
        android:layout_height="200dp"  
        android:layout_alignLeft="@+id/button1"  
        android:layout_below="@+id/button1"  
        android:background="#ffff0000"  
        >  
    </RelativeLayout>  
  
</RelativeLayout>  

res/layout/textview.xml

<?xml version="1.0" encoding="utf-8"?>  
<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"  
    android:layout_centerInParent="true"  
    android:background="#ff00ff00"  
    android:visibility="visible"  
    android:text="hello world!"/>  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值