2012-01-19 9 views
2

私は何かが見つからないかもしれませんが、特定のレイアウトパラメータで指定されたインデックスのビューにサブビューを追加する方法が見つかりません。 addView(View v, LayoutParams lp)があり、addView(View v, int index)がありますが、両方を指定する方法が見つかりません。Android - 特定のインデックス(コード内)のレイアウトパラメータでサブビューを追加

私には何が欠けていますか?

答えて

4

なぜ、両方の方法があります:addView(View child, int index, ViewGroup.LayoutParams params)Hereです。

+0

本当に、なぜ私の食は不平を言うのですか? –

1

これは例です。どのビューでも進行状況インジケーターを表示するために使用します。ここで

final ViewGroup rootFrameLayout = (ViewGroup) activity.getWindow().peekDecorView(); 
LayoutInflater li = LayoutInflater.from(activity); 
progressView = li.inflate(R.layout.progressindicator, null); 
rootFrameLayout.addView(progressView); 

、あなたがメソッドを呼び出すことができますが、動的にレイアウトを変更したいだけの場合には、サンプルレイアウトファイル(IDはR.layout.progressindicatorある)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<ProgressBar 
    android:id="@+id/progressBar1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="3dp" 
    android:layout_toRightOf="@+id/progressBar1" 
    android:maxLines="3" 
    android:lines="3" 
    android:background="#88000000" 
    android:textColor="#ffffff" 
    android:text="@string/waitingForServer" /> 

ですレイアウト内の要素のこのように:

TextView textView = (TextView) progressView.findViewById(R.id.textView1); 
textView.setMaxLines(lines); 
textView.setLines(lines); 
textView.setVisibility(View.VISIBLE); 
+0

これがどのように質問に答えるか分かりません。レイアウトパラメータをどこで指定していますか? –

+0

@AleksGレイアウトファイルを追加し、その使用方法を説明しました。 –

+0

私はまだこのすべてが質問に答える方法を理解していません。私はレイアウトファイルを持っていません - 私はコード内でその場でビューを作成します。レイアウトパラメータを指定しながら、ビューを親ビューに追加する必要があります。あなたの答えは、これを行うことを試みません。 –

関連する問題