どのように膨らみつのビューのレイアウト
-
22-09-2019 - |
質問
いレイアウト定義された形式をサポートしています。を含んでいま
<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(...)
られましたが、いずれも現代美術館などがあります。
解決
私は確かに私はあなたのquestion-続いていませんよ、あなたはRelativeLayoutに子ビューを添付しようとしていますか?あなたはの線に沿って何かをしたいので、もします:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
他のヒント
あなたは、XMLリソースを膨らませます。 LayoutInflaterドキュメントを参照してください。
あなたのレイアウトがされている場合、の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);
後半答えものの、 しかし、この
を取得する1つの方法を追加したいと思いますLayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
item
はあなたが子のレイアウトを追加したい親のレイアウトです。
それは、XMLから膨張している子ビューがのViewGroupレイアウトに追加する場合は、あなたがのViewGroupのどのようなタイプの手がかりとコール膨らませる必要があること、古い記事だにもかかわらず、これに追加すると便利ですに追加されようとしています。同様ます:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
インフレート方法は、非常に過負荷とドキュメントの使用のこの部分を説明しています。私は、単一のビューが、私はこの種の変更をしたまできちんと親に揃えていなかったXMLから膨張問題を抱えています。
でももっと簡単な方法を使用することです。
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);
あなたが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年前のことを知っているが、まだ言及する価値)
あなたは、単一のビューの複数の時間を追加したい場合は、あなたが使用する必要があります。
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);
}
それはすべての準備ができて追加したビューの例外がスローされます。
AttachToRoot Trueに設定
もし指定ボタンをXMLのレイアウトファイルとそのレイアウトの幅およびレイアウトの高さに設定match_parent.
<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);
本ソリューションでは、ます!
私は私のためのユニークな事情のため、このエラーで最も困難な時間を持っていましたが、最終的には解決策を見つけます。
私の状況:私は私のメインのアクティビティビューでボタンをクリックしたとき、私は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();
あなたはRelativeLayoutに子ビューを添付しようとしている場合は?次によって行うことができます。
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);
みのあるコード:
- また膨らませるレイアウト:
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
- したい場合に膨らませるレイアウトにコンテイナー(親のレイアウト):
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.
Kotlinでは、あなたが使用することができます:
val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
[your_main_layout].apply {
//..
addView(content)
}
私は、このためのコードのスニペットの下に使用されていたし、それは私のために働いています。
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);
いいえ
のように、それは単純な任意の複雑さを必要とします setContentView(R.layout.your_layout);