0

複数の LinearLayouts を xml で宣言されたものに追加しようとしています。誰もがコードで編集される 3 つの textView を持っています。私の問題は、コードで xml レイアウトを View オブジェクトに拡張すると、すべてのマージンが無視されることです。2 番目の質問: ID を textViews に動的に設定し、その中のテキストを編集するにはどうすればよいですか?

膨張している LinearLayout xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/pointsAwaiting"
    android:layout_marginTop="40dp"
    android:background="@drawable/background_blue"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#FFFFFF"
            android:textSize="20dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:textColor="#FFFFFF"
            android:textSize="15dp" />

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:padding="10dp"
        android:textColor="#FFFFFF"
        android:textSize="20dp" />

</LinearLayout>

彼は次のコードに膨らんでいます。

<

ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">

<LinearLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">

<LinearLayout
            android:id="@+id/historyView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">



        </LinearLayout>

    </LinearLayout>

</ScrollView>

そして最後にJavaコード:(ループカウンターは例です)

LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);

            for (int i=0; i<=2; i++){
                View layout = View.inflate(this, R.layout.history_bar, null);
                mainView.addView(layout);
            }
4

1 に答える 1

0

すべてのマージンが無視されている理由は、ここでインフレータに null を渡しているためです。

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

これを行うと、ビューのすべての LayoutParams が破棄されます。次のものに置き換える必要があります。

View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);

TextViews にテキストを設定するには、それぞれに異なる ID を設定する必要はありません。それぞれに ID を指定するだけです。次に、ループで次のようなことができます。

List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this); 

for (int i=0; i<=2; i++){

    View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
    mainView.addView(layout);

    TextView textView = (TextView) layout.findViewById(R.id.text1);
    textViews.add(textView);
}

次に、リスト内の各 TextViews への参照を取得します

于 2016-05-12T11:48:23.257 に答える