如何为Android Layout文件创建可重用的xml包装器

Gen*_*ari 9 xml layout android include

除了一个部分,我有几个大致相同的布局文件.有没有办法让我可以在一个地方拥有共同的XML; 而不是复制/粘贴,并且当我想要进行1次更改时必须更新一堆文件?

我知道我可以从其他XML文件中包含XML,但是公共代码不是内部控件; 它是外包装; 所以包括不起作用.基本上,我有一堆文件,看起来像这样:

<LinearLayout
    android:id="@+id/row"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView android:layout_height="26dp"
           android:id="@+id/checkImage"
           android:layout_width="26dp"
           android:layout_alignParentTop="true"
           android:scaleType="fitCenter"/>

    <!-- Different types of views go here depending on which layout file it is -->

    <ImageButton android:layout_height="fill_parent"
             android:id="@+id/playButton"
             android:layout_width="42dp"
             android:src="@drawable/play_button"
             android:scaleType="center"

             android:background="#00000000"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

基本上,我想做ASP.Net对Master Pages的处理.这有什么选择吗?

Ahm*_*agh 6

解决方案非常简单.

您需要将函数SetContentView中的" Activity "类扩展onCreate()为基本xml布局,还需要覆盖基本Activity类中的setContentView

例如:

1. base_layout.xml使用以下代码创建

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
           <ImageView 
               android:id="@+id/image_view_01"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:maxHeight="50dp" />
   </LinearLayout>

   <LinearLayout 
       android:id="@+id/base_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
   </LinearLayout>
</LinearLayout>    
Run Code Online (Sandbox Code Playgroud)
  1. 创建 BaseActivity.java
public class BaseActivity extends Activity {
    ImageView image;
    LinearLayout baseLayout;     

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        super.setContentView(R.layout.base_layout);    

        this.image = (ImageView) this.findViewById(R.id.image_view_01);
        this.baseLayout = (LinearLayout) this.findViewById(R.id.base_layout);

        this.image.setImageResource(R.drawable.header);
    }

    @Override
    public void setContentView(int id) {
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(id, this.baseLayout);
    }
}
Run Code Online (Sandbox Code Playgroud)

SomeActivity.java

public class SomeActivity extends BaseActivity {

    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.some_layout);

       //rest of code
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我唯一注意到的是当请求进度条(requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS))时,这需要在调用super.onCreate之前完成.我想这是因为在调用此函数之前还没有任何东西可以绘制.

这对我很有用,希望你会发现这对你自己的编码很有用.