gpt4 book ai didi

java - 以编程方式添加带有约束的textView

转载 作者:行者123 更新时间:2023-12-02 11:06:22 25 4
gpt4 key购买 nike

我有一个由 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"

谁能帮帮我吗?

最佳答案

 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>

关于java - 以编程方式添加带有约束的textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50929170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com