如何解决如何为Android布局文件创建可重用的xml包装器
|| 除了一部分外,我有几个布局文件几乎相同。有没有一种方法可以将通用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>
基本上,我想做ASP.Net对母版页所做的事情。有什么选择吗?
解决方法
解决方案非常简单。
您需要在
onCreate()
函数SetContentView中将\“ Activity \”类扩展到基本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>
创建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之前完成此操作。我认为这是因为调用此函数之前无法绘制任何内容。
这对我来说非常有用,希望您会在自己的编码中发现它有用。
, 也许您可以使用一个主布局XML文件,然后根据需要通过代码动态添加/删除其他小部件。
, 我正试图做到这一点-我想要一个视图,该视图的左边有一个按钮,右边有一个按钮,但是中间可以有任意内容(取决于包含它的人)。基本上是一个自定义视图组,该视图组可以在XML布局中具有子视图,并将这些子视图包装在另一个XML布局中。这是我的做法:
top_bar.xml:这表示包装东西的常用布局。请注意,ID为“ addChildrenHere \”的LinearLayout(可以是任何布局)-稍后将进行引用。
<?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:主布局。这包括带有几个子级的自定义视图组(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,params);
}
}
}
据我所知,这行得通。它在Eclipse布局编辑器中不能很好地工作(我不太确定问题是什么),但是您可以很好地查看布局。更改WrappedLayout的子级似乎需要手动编辑XML。
, 您是否看过应用样式和主题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。