Android 如何使用布局为一个视图充气

Android 如何使用布局为一个视图充气,android,android-layout,layout-inflater,android-inflate,Android,Android Layout,Layout Inflater,Android Inflate,我有一个用XML定义的布局。它还包括: <RelativeLayout android:id="@+id/item" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 但是没有一个工作正常。您夸大了XML资源。看 如果您的布局位于mylayout.xml中,您可以执行以下操作: View view; LayoutInflater inflater = (La

我有一个用XML定义的布局。它还包括:

<RelativeLayout
    android:id="@+id/item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

但是没有一个工作正常。

您夸大了XML资源。看

如果您的布局位于mylayout.xml中,您可以执行以下操作:

View view; 
LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);

RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);

我不确定我是否遵循了您的问题-您是否试图将子视图附加到RelativeLayout?如果是这样,您希望按照以下思路做一些事情:

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

尽管这是一篇旧文章,但如果要将从xml扩展的子视图添加到视图组布局中,则需要调用inflate,并提供将添加到哪种类型的视图组的线索,这是很有帮助的。比如:

View child = getLayoutInflater().inflate(R.layout.child, item, false);
充气方法是非常重载的,并在文档中描述了这部分用法。我遇到了一个问题,在我做了这种更改之前,从xml扩展的单个视图在父视图中没有正确对齐。

虽然回答晚了, 但我想补充一个方法来实现这一点

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.mylayout, item );

其中,
item
是要在其中添加子布局的父布局。

如果您不在活动中,则可以使用
LayoutInflater
类中的静态
from()
方法来获取
LayoutInflater
,或者从上下文方法
getSystemService()
请求服务:

LayoutInflater i;
Context x;       //Assuming here that x is a valid context, not null

i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);

(我知道这几乎是4年前的事了,但仍然值得一提)

如果你想多次添加单视图,那么你必须使用

   layoutInflaterForButton = getActivity().getLayoutInflater();

 for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
        FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
        btnContainer.addView(btnView);
    }

for(int noOfButton=0;noOfButton<5;noOfButton++){
btnContainer.addView(btnView);
}

然后它将抛出所有已添加视图的异常。

更简单的方法是使用

View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
布局膨胀

View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);

由于我独特的环境,我经历了这个错误最艰难的时刻,但最终找到了解决办法

我的情况:我使用的是一个单独的视图(XML),它包含一个
WebView
,当我单击主活动视图中的按钮时,它会在
AlertDialog
中打开。但不知何故,
WebView
属于主活动视图(可能是因为我从这里提取了资源),所以在我将其分配给
AlertDialog
(作为视图)之前,我必须获取
WebView
的父视图,将其放入
视图组中,然后删除该
视图组上的所有视图。这起作用了,我的错误消失了

// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);

// more code...
。。。。稍后,在我加载我的
WebView
之后

// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();

// put WebView in Dialog box
alert.setView(webView);
alert.show();

我使用了下面的代码片段,它对我很有用

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);

使用Kotlin,您可以使用:

val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)

[your_main_layout].apply {
    //..
    addView(content)
}

如果要将子视图附加到RelativeLayout?你可以按照下面的方法做

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);

将AttachToRoot设置为True

试想一下,我们在XML布局文件中指定了一个按钮,其布局宽度和布局高度设置为与父级匹配

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>
希望此解决方案对您有效

请尝试以下代码:

  • 如果您只想扩大布局,请执行以下操作:
  • View View=LayoutInflater.from(context).充气(R.layout.your_xml_layout,null);//膨胀xml布局的代码
    
    RelativeLayout项=view.findViewById(R.id.item)我的主视图已膨胀。问题是我想从其他XML文件中膨胀部分内容。此项RelativeLayout是较大版面的一部分,我想用另一个XML文件中的版面填充它。嗯,很抱歉,我不知道:(.您是否在主视图中使用类似的内容来包含其他布局?不,这是一个简单的布局。我想我将只通过编程添加元素,而不使用XML。谢谢帮助!我收到一个错误:02-25 22:21:19.324:error/AndroidRuntime(865):原因:java.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。现在刚刚尝试此操作,我需要:View child=getLayoutInflater().inflate(R.layout.child,null);问题不在于代码。代码很容易理解。xml文件是在哪里生成的。类型是主题样式资源吗?我想包括我在内的人的问题是我们将xml文件放在哪里这不是完美的。应该使用充气机的3参数版本-原因如下:答案不正确,即使有效。请检查o但在这篇文章中(即使是以后的文章),我很难实现你的答案。我尝试了你上面提到的代码,但将“item”替换为:“R.layout.activity\u layout”,我得到了一个“没有适用于(int,int)的方法”我可以麻烦你考虑一下吗?@Kyle-甚至更晚。你不能使用
    R
    资源作为对象的引用,它们只是
    int
    s。你必须使用
    findViewById
    R
    资源传递给它,然后键入cast将其转换为你想要的对象。然后你可以在函数调用中使用它,比如
    inflate
    (即
    ViewGroup项=(ViewGroup)findViewById(R.layout.activity_layout);
    …然后您可以像上面那样使用
    item
    。)我想我应该使用
    RelativeLayout
    而不是上面的示例中的
    ViewGroup
    ,因为你不能实例化基类
    ViewGroup
    。5分钟的编辑规则让我明白了。LOL是否可以隐藏父视图并只显示子视图谢谢,这个方法帮助我解决了无法使用getContext()的问题在上一个示例中,因为我使用的是片段。最后,谢谢:)这允许将多个视图添加到单个父视图,但无需担心LayoutParams(从XML读取)。干得好!使用
    View.inflate()
    是最好的方法,没有不必要的
    LayoutFlater
    getSystemService(Context.LAYOUT\u filler\u SERVICE)
    或所有这些讨厌的东西!:D
    val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
    
    [your_main_layout].apply {
        //..
        addView(content)
    }
    
    RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
    View child = getLayoutInflater().inflate(R.layout.child, item, true);
    
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/custom_button">
    </Button>
    
    LayoutInflater inflater = LayoutInflater.from(getContext());
    inflater.inflate(R.layout.yourlayoutname, this);