Android 在自定义视图类中膨胀 xml 布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11029192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:47:49  来源:igfitidea点击:

Inflating a xml layout in a custom View class

androidviewandroid-inflate

提问by LuminiousAndroid

I have a class View1that extends View. I want to inflate R.layout.test2.xmlin this class View1. I have put a following code in this class

我有一个View1扩展View. 我想R.layout.test2.xml在这门课上膨胀View1。我在这个类中放置了以下代码

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);
    }
}

From another class HomeI want this inflated view to be there for some circumstances , In the Homeclass I wrote the following code:

在另一个类中,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);
    }
}

But as I run my project the activity doesn't show me anything.

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

回答by Luksprog

You can't add views to the Viewclass instead you should use ViewGroupor one of its subclasses(like Linearlayout, RelativeLayoutetc). Then your code will be like this:

您不能添加欣赏到View类,而不是你应该使用ViewGroup或它的子类(如之一LinearlayoutRelativeLayout等等)。那么你的代码将是这样的:

public class View1 extends LinearLayout {

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

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

Also the line:

还有这一行:

mInflater.inflate(R.layout.test2, null, false)

doesn't add anything to the current view(because of the falseparameter) , it just inflates a Viewfrom a xml layout.

不会向当前视图添加任何内容(因为false参数),它只是View从 xml 布局中膨胀 a 。

回答by Aracem

Use this

用这个

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

You must to use this, not null, and change the falseparameter (boolean AttachToRoot ) to true

您必须使用this,而不是 null,并将false参数(布尔值 AttachToRoot )更改为true

回答by Bharat Sharma

Use the code below to inflate your layout, then you can use that view for any purpose. This will give you the most parent layout of your XML file. Type cast and use it accordingly.

使用下面的代码来扩展您的布局,然后您可以将该视图用于任何目的。这将为您提供 XML 文件的最父布局。键入 cast 并相应地使用它。

View headerView = View.inflate(this, R.layout.layout_name, null);

回答by d.k.

You are adding in home activity blank view. Because in View1 class you have only inflate view but not add anywhere.

您正在添加家庭活动空白视图。因为在 View1 类中,您只有膨胀视图,但不能在任何地方添加。

回答by Marius

You have to use a ViewGrouplike FrameLayoutand do:

您必须使用ViewGroup喜欢FrameLayout并执行以下操作:

public class View1 extends FrameLayout {

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

In the layout XML, use the <mergetag to not just add your view1layout to the root layout which means we have an empty FrameLayoutand your defined view beside each other.

在布局 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>

See http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

回答by chris

In kotlin
LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)

在科特林
LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)