如何为Android布局文件创建可重用的XML包装器

9

我有几个布局文件,它们大部分都相同,除了一个部分。有没有一种方法可以将公共的XML代码放在一个地方,而不是复制/粘贴,并在想要进行1次更改时必须更新一堆文件?

我知道可以从其他XML文件中包含XML,但是公共代码不是内部控件; 它是外层包装器; 因此无法使用include。基本上,我有一堆文件都长这样:

<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>

基本上,我想做的是ASP.Net使用主页面所做的事情。有这个选项吗?
4个回答

6
解决方案非常简单。 您需要扩展“Activity”类,在onCreate()函数中将SetContentView设置为基本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>    
  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);
    }
}

以及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
    }
}

到目前为止,我唯一注意到的是,在请求进度条时(requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)),必须在调用super.onCreate之前完成。我认为这是因为在调用此函数之前无法绘制任何内容。
这对我非常有效,希望您在自己的编码中也能发现这个有用的技巧。

那救了我的一天。正是我正在寻找的。应该标记为正确答案。 - Joehl

1

我试图做到这一点 - 我想要一个视图,在左侧有一个按钮,在右侧有一个按钮,但是中间可以有任意内容(取决于谁在包含它)。基本上是一个自定义视图组,可以在XML布局中具有子视图,并将这些子视图与另一个XML布局包装。以下是我的做法:

top_bar.xml:表示用来包装内容的常见布局。请注意LinearLayout(可以是任何布局)带有ID“addChildrenHere” -稍后会引用它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topBarLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />

    <LinearLayout
        android:id="@+id/addChildrenHere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />

</LinearLayout>

main.xml: 主要布局。其中包括一个自定义的viewgroup(WrappedLayout)和几个子项。请注意它声明了一个自定义的XML命名空间,并在WrappedLayout标签上设置了两个自定义属性(这些属性指定应该用哪个布局来包装子项,以及此节点中的子项应该放置在那个布局中的哪个位置)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:karl="http://schemas.android.com/apk/res/karl.test"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <karl.test.WrappedLayout
        android:id="@+id/topBarLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        karl:layoutToInflate="@layout/top_bar"
        karl:childContainerID="@+id/addChildrenHere">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a child of the special wrapper."
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is another child; you can put anything here."
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </karl.test.WrappedLayout>

</LinearLayout>

attrs.xml:这个文件放在res/values目录下。它定义了上面XML中使用的自定义XML属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="WrappedLayout">
    <attr name="layoutToInflate" format="integer"/>
    <attr name="childContainerID" format="integer"/>
  </declare-styleable>
</resources>

最后,WrappedLayout.java:这个文件处理读取自定义属性,并进行一些技巧性的操作,使addView()实际上将视图添加到不同的位置。
package karl.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class WrappedLayout extends FrameLayout
{

  ///Attempts to add children to this layout will actually get forwarded through to mChildContainer.
  ///This would be final, but it's actually used indirectly by the constructor before it's initialised.
  private ViewGroup mChildContainer;

  public WrappedLayout(Context context, AttributeSet attrs)
  {
    super(context, attrs);

    //read the custom attributes
    final int layoutToInflate;
    final int childContainerID;
    {
      final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.WrappedLayout);

      layoutToInflate  = styledAttributes.getResourceId(R.styleable.WrappedLayout_layoutToInflate, 0);
      childContainerID = styledAttributes.getResourceId(R.styleable.WrappedLayout_childContainerID, 0);

      styledAttributes.recycle();
    }

    if(layoutToInflate == 0
    || childContainerID == 0)
    {
      Log.e("Error", "WrappedLayout.WrappedLayout(): Error reading custom attributes from XML. layoutToInflate = " + layoutToInflate + ", childContainerID =" + childContainerID);
    }
    else
    {
      //inflate the layout and (implicitly) add it as a child view
      final View inflatedLayout = View.inflate(context, layoutToInflate, this);

      //grab the reference to the container to pass children through to
      mChildContainer = (ViewGroup)inflatedLayout.findViewById(childContainerID);
    }
  }

  ///All the addView() overloads eventually call this method.
  @Override
  public void addView(View child, int index, ViewGroup.LayoutParams params)
  {
    if(mChildContainer == null)
    {
      //still inflating - we're adding one of the views that makes up the wrapper structure
      super.addView(child, index, params);
    }
    else
    {
      //finished inflating - forward the view through to the child container
      mChildContainer.addView(child, index, params);
    }
  }

}

据我所知,这个可以工作。但是在Eclipse布局编辑器中效果不太好(我不确定问题出在哪里),但您可以正常查看布局。更改WrappedLayout的子项似乎需要手动编辑XML。


1
也许你可以使用一个主要的布局XML文件,然后根据需要通过代码动态添加/删除其他小部件。

是的,我在考虑这样的事情。但我可以在XML中定义这些小部件,然后从那里拉出来吗?还是我必须像对待我的主文件一样填充这些XML文件,然后找到其中的控件并将它们添加到我的视图中? - GendoIkari
@Gendolkari 希望这个示例能够帮到你。如果需要更复杂的内容,可以阅读这篇文章或者这篇文章 - marchica

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接