在接触控件中,常常可以看到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!"/>