在自定义View类中填充XML布局

40

我有一个继承了 View 的类 View1。 我想在这个 View1 类中填充 R.layout.test2.xml。 我已经在这个类中放置了以下代码

public class View1 extends View {

    View view;
    String[] countries = new String[] {"India", "USA", "Canada"};

    public View1( Context context) {
        super(context);
        LayoutInflater  mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=mInflater.inflate(R.layout.test2, null, false);
    }
}

我想从另一个名为Home的类中,针对某些情况引入该充气视图,我在Home类中编写了以下代码:

public class Home extends Activity{

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

    public void CreateView() {
        LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
        View1 view = new View1(Home.this);
        lv.addView(view);
    }
}

但是当我运行我的项目时,该活动没有显示任何内容。

6个回答

53

你不能向 View 类中添加视图,相反应该使用 ViewGroup 或其子类(如 LinearLayoutRelativeLayout 等)。然后你的代码将会像这样:

    public class View1 extends LinearLayout {

        View view;
        String[] countries = new String[] {"India", "USA", "Canada"};

        public View1( Context context) {
            super(context);
            inflate(context, R.layout.test2, this);
        }
    }

你能说说为什么扩展View后它什么也没显示吗? - LuminiousAndroid
1
@Kabir121,“View”类不支持向其中添加其他视图,它代表一个单独的个体“View”(例如,如果您想构建一个圆形的“Button”),而不是一个组(例如,在您膨胀布局文件时)。 - user
1
这个解决方案应该是可行的,但也意味着会有一些视图组始终只有一个视图,这将导致布局阶段需要更多的工作。我可以说建造者模式可能更好,但那样你就错过了扩展视图的整个重点。 - android developer
你可以膨胀一个视图,只需要在构造函数中传递ViewGroup即可。 确保有一个ViewGroup可以传递;一些布局。 - Malba

12

使用这个

    LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
    li.inflate(R.layout.test2, **this**, true);

你必须使用this,而不是null,并将false参数(boolean AttachToRoot)更改为true


10
要实现这个,你需要从ViewGroup类派生子类,而不是从View类派生。 - spookypeanut

4
你需要使用像 FrameLayout 这样的 ViewGroup, 然后进行以下操作:
public class View1 extends FrameLayout {

    public View1(Context context) {
        super(context);
        inflate(context, R.layout.view1, this);
    }
}

在布局XML中,使用<merge标签,不仅将view1布局添加到根布局中,这意味着我们有一个空的FrameLayout和您定义的视图并排。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="content" />

</merge>

请查看http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/以了解与自定义视图相关的布局充气技巧。


3
使用以下代码来填充您的布局,然后您可以将该视图用于任何目的。这将为您提供XML文件中最父级的布局。进行类型转换并相应地使用它即可。
View headerView = View.inflate(this, R.layout.layout_name, null);

如果我在View1类中有一个列表视图,然后在列表项点击时我尝试填充新的视图,但它没有填充新的视图。请有人帮忙。 - LuminiousAndroid
你的列表项单击事件是在自定义适配器中还是在活动中? - Bharat Sharma

1
你正在添加主页活动的空白视图。因为在View1类中,你只是填充了视图,但没有将其添加到任何地方。

0

在 Kotlin 中
LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)


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