如何夸大一个视图与布局

问题描述 投票:205回答:16

我在XML中定义的布局。它还包含:

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

我想这种膨胀与RelativeView其他XML布局文件。我可以用根据情况不同的布局。我应该怎么办呢?我尝试不同的变化

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

但他们没有工作得很好。

android android-layout layout-inflater android-inflate
16个回答
375
投票

我不知道我按照您的question-你试图连接一个子视图到RelativeLayout的?如果是这样,你想做的事线的东西:

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

2
投票

我有困难的时候与这个错误,因为我的特殊情况,但最后还是找到了解决办法。

我的情况:然后我使用的持有WebView一个单独的视图(XML),在AlertDialog打开,当我在我的主要活动视图中单击一个按钮。但不知何故或其他的WebView属于主要活动视图(可能是因为我拉从这里的资源),这样对我分配到我的AlertDialog之前(如视图),我必须让我的WebView的父母,把它成ViewGroup,然后删除对ViewGroup各方面的意见。这个工作,我的错误就走开了。

// 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();

2
投票

如果你试图连接一个子视图到RelativeLayout的?您可以通过以下做

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

2
投票

试试这个代码:

  1. 如果你只是想抬高你的布局:

View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);   
  1. 如果你想在容器(父布局)夸大你的布局:

LinearLayout parent = findViewById(R.id.container);        //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false); 
RelativeLayout item = view.findViewById(R.id.item);       //initialize layout & By this you can also perform any event.
parent.addView(view);             //adding your inflated layout in parent layout.

1
投票

随着科特林,您可以使用:

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

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

-1
投票

下面的代码片段我用这个和它的工作对我来说。

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

-1
投票

无需任何复杂,它只是简单

 setContentView(R.layout.your_layout);

102
投票

你膨胀的XML资源。见LayoutInflater doc

如果你的布局是在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);

25
投票

虽然晚了答案,但想补充一点,一个办法让这个

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

其中item是要增加一个子布局的父布局。


18
投票

这是有帮助添加到这一点,即使它是一个老帖子,如果正从XML充气子视图将被添加到一个ViewGroup中的布局,你需要调用它会是什么类型的ViewGroup的线索膨胀要添加到。喜欢:

View child = getLayoutInflater().inflate(R.layout.child, item, false);

的充气方法是相当重载,并描述在文档的使用的这部分。我不得不在那里,直到我提出这种类型的变化从XML膨胀的单一视图没有在父对准正确的问题。


11
投票

更简单的方法是使用

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

8
投票

布局通胀

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

6
投票

如果您在活动是不是你可以使用静态from()方法从LayoutInflater类来获得一个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年前,但仍然值得一提)


3
投票

如果你想添加一个单一视图多个时间,那么你必须使用

   layoutInflaterForButton = getActivity().getLayoutInflater();

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

如果你喜欢

   layoutInflaterForButton = getActivity().getLayoutInflater();
    FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);

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

那么它会抛出一切准备添加的视图的例外。


3
投票

AttachToRoot设置为True

试想,我们在其布局的宽度和布局高度设置为match_parent一个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>

在这个按钮点击事件,我们可以设置下面的代码膨胀布局在此活动。

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);

希望这个解决方案适用于您!

© www.soinside.com 2019 - 2024. All rights reserved.