将视图膨胀到 LinearLayout 并在此视图中编辑组件

Inflating View into LinearLayout and editing cmponents in this view

我正在尝试将多个 LinearLayout 添加到 xml 中声明的一个中。每个人都有 3 个 textView,将在代码中进行编辑。我的问题是,当我在代码中将 xml 布局扩展到 View 对象时,所有边距都被忽略了。我的第二个问题: 如何动态设置 ids 到 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);
            }

您的所有边距都被忽略的原因是您在此处将 null 传递给充气器:

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

当您这样做时,您视图的所有 LayoutParams 都将被丢弃。您应该将其替换为:

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

要在 TextView 中设置文本,您不需要为每个设置不同的 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);
}

然后您将引用列表中的每个 TextView