Android layout Android使布局可重用
我对复制这段代码感到厌烦Android layout Android使布局可重用,android-layout,Android Layout,我对复制这段代码感到厌烦 <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="6dp" android:orientation="vertical"> <!-- stuff goes here -- usually
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical">
<!-- stuff goes here -- usually more layouts -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:background="@color/background"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:layout_marginBottom="6dp"
android:text="Title"
android:layout_marginTop="-3dp"
/>
</RelativeLayout>
有没有一种方法可以让它重复使用,比如:
<BoxWithTitleLayout
android:layout_weight="1"
title="Title"
>
<!-- stuff -->
</BoxWithTitleLayout>
您可以使用[!LayoutInflater]()根据布局模板创建视图,然后将其插入到需要的视图中
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView)
v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup)
findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
可以使用[!LayoutInflater]()基于布局模板创建视图,然后将其插入到需要的视图中
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView)
v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup)
findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));