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

时间:2016-05-12 11:30:46

标签: android android-layout views android-linearlayout layout-editor

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

和finnaly 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);
            }

1 个答案:

答案 0 :(得分:0)

你忽略所有边距的原因是你在这里将null传递给你的inflater:

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设置一个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的引用
相关问题