自定义视图:如何设置根布局

3

我正在尝试创建一个自定义视图(扩展RelativeLayout),它包装了很多其他视图。 我想在xml布局文件中创建这些子视图。现在我想知道如何填充该布局并在我的自定义视图中使用它。像这样的东西非常好(在我的自定义视图内部):

RelativeLayout rootLayout = (RelativeLayout) inflater.inflate(my xml file)
this.setContenView(rootLayout);

不幸的是,这只在活动中才可能实现。是否有类似的方法适用于视图?

编辑: 我不想使用View.addView(rootLayout),因为它会添加另一个视图层次结构,这是不必要的。

3个回答

4
您可以尝试在布局中使用<merge>标签作为根元素,并在自定义RelativeLayout中使用this作为父级并将attachToRoot设置为true来膨胀它。然后您就不需要调用addView了。
这里有一个类似的示例,使用LinearLayout(页面底部),也适用于RelativeLayout

1
使用以下内容:
View v =getLayoutInflater().inflate(R.layout.mylayout,null);
// inflate mylayout.xml with other views
CustomRelativeLayout cs = new CustomRelativeLayout(this);
// CustomRelativeLayout is a class that extends RelativeLayout
cs.addView(v);  // add the view to relative layout
setContentView(cs); // set the custom relative layout to activity

例子:

示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="111dp"
        android:text="TextView" />

</RelativeLayout>

SView
public class SView extends RelativeLayout {

    Paint p,paint; 
    public SView(Context context) {
        super(context);
        TextView tv = new TextView(context);
        tv.setText("hello");
        this.addView(tv);
    }
}

在MainActivity中。
View v =getLayoutInflater().inflate(R.layout.mylayout,null);
SView cs = new SView(this);
cs.addView(v);
setContentView(cs);

Snap

enter image description here

编辑:

如果您希望在CustomRelative布局中进行填充

在构造函数中

LayoutInflater inflater = LayoutInflater.from(context);
View v =inflater.inflate(R.layout.mylayout,null);
TextView tv = new TextView(context);
tv.setText("hello");
this.addView(tv);
this.addView(v);

1
这样我将获得一个额外的嵌套级别,但实际上并不需要 - 我将有一个RelativeLayout(cs)和一些额外的根布局(mylayout),它们将被嵌套。其中一个是无用的。 - stoefln
@stoefln,我可能误解了你的问题,请更清楚地表达或者发布一些代码片段来指示你已经完成了什么。其中一个是无用的。实际上,你可以动态创建一个TextView,并将它们添加到自定义相对布局中,在这种情况下,你不需要我的布局。 - Raghunandan
@stoefln CustomRelativeLayout cs = new CustomRelativeLayout(this); 现在你可以像这样创建文本视图 TextView tv = new TextView(this); tv.setText("hello"); 然后将文本视图添加到相对布局中 cs.addView(tv);,不需要使用 mylayout。 - Raghunandan
如果您希望在自定义视图中进行膨胀,请查看编辑。但这仍然是嵌套的。您可以添加文本视图,然后将充气的自定义视图添加到相对布局中。此自定义相对布局可以设置为活动。 - Raghunandan
是的,我知道你告诉我的一切。问题是:我正在寻找一种替代方案。我想要“设置”一个视图,而不是“添加”一个视图。 - stoefln
显示剩余4条评论

0

在您的视图中,您可以从上下文获取布局填充器,填充子项并将它们添加到thisRelativeLayout的子类)中。

final LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View child = inflater.inflate(R.layout.custom_layout, this, false);
// Then add child to this (subclass of RelativeLayout)
this.addView(child);

编辑:

上面的代码展示了如何在自定义视图中填充子视图。 这个链接 展示了如何将自定义视图本身插入到 XML 布局中。


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