以编程方式添加具有约束条件的textView

时间:2018-06-19 13:02:36

标签: java android

我有一个由xml定义的ScrollView。在其中,我想要有两个textView,如这张照片所示:

enter image description here

在xml中执行此操作就可以了。但是如何以编程方式添加它们?没有两个TextView示例的代码如下所示:

.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    tools:context=".InventoryActivity">

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/inventorylayoutbg" />

    <ImageView
        android:id="@+id/imageView33"
        android:layout_width="61dp"
        android:layout_height="59dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="closeDialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.254"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.927"
        app:srcCompat="@drawable/blank" />

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="326dp"
        android:layout_height="374dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView9">

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

</android.support.constraint.ConstraintLayout>

.java文件

package com.example.jsciampi.tapper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class InventoryActivity extends AppCompatActivity {
    //Form
    ScrollView sv;
    LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = (int)(dm.widthPixels*.9);
        int height = (int)(dm.heightPixels*.8);

        getWindow().setLayout(width,height);

        sv = findViewById(R.id.sv);
        ll = findViewById(R.id.ll);
        for(int i = 0; i < 5; i++){
           Button b = new Button(InventoryActivity.this);

           //Add Constraint there

            ll.addView(b);
        }
    }

    public void closeDialog(View view) {
        finish();
    }
}

第二个TextView具有以下xml配置:

<TextView
    android:id="@+id/textView2"
    android:layout_width="324dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/sv"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/firstText"
    app:layout_constraintVertical_bias="0.0" />

我真的只需要在这里将xml转换为java即可:

app:layout_constraintBottom_toBottomOf="@+id/sv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aads"
        app:layout_constraintVertical_bias="0.0"

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

 for (int i = 0; i < 5; i++) {
         View row = LayoutInflater.from(this).inflate(R.layout.image_text_row,null,false);
         ll.addView(row);
        }

将以下布局添加到名为image_text_row.xml的文件中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:src="@android:drawable/ic_dialog_email" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Text"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</LinearLayout>