gpt4 book ai didi

java - 无法通过 Java 在 XML 资源文件中复制布局外观

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:24 24 4
gpt4 key购买 nike

我是 Android 开发新手。我在网上搜索答案,但一无所获。我正在尝试在 TableLayout 中动态创建行,如下所示:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.me.testdesign.MainActivity">

<TableLayout
android:id="@+id/tableLayout"
android:layout_width="368dp"
android:layout_height="345dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#12dd12"
android:text="static textview"
android:textSize="12sp"
android:layout_marginRight="10dp"/>

<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="static btn"
android:textSize="12sp"
android:background="#12dd12"/>
</TableRow>
</TableLayout>

</android.support.constraint.ConstraintLayout>

这个 XML 文件给出了我想要的每一行的外观,如下面的快照所示:

enter image description here

下面是动态创建附加行的 Java 代码:

    public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show(); }
});

TableLayout.LayoutParams tvlparams =
new TableLayout.LayoutParams(0, TableLayout.LayoutParams.MATCH_PARENT);
tvlparams.weight = 3;
TextView tv = new TextView(this);
tv.setBackgroundColor(0xff12dd12);
tv.setText("dynamic textview");
tv.setLayoutParams(tvlparams);

TableLayout.LayoutParams btnlparams =
new TableLayout.LayoutParams( 0, TableLayout.LayoutParams.MATCH_PARENT);
btnlparams.weight = 2;
btnlparams.rightMargin = 10;
Button btn = new Button(this);
btn.setText("dynamic btn");
btn.setBackgroundColor(0xff12dd12);
btn.setLayoutParams(btnlparams);

TableRow row = new TableRow(this);
row.addView(tv);
row.addView(btn);
TableLayout TL = (TableLayout) findViewById(R.id.tableLayout);
TL.addView(row);
}
}

上面的Java代码似乎什么也没做!为什么我没有获得新行?

但是,如果我注释掉 tv.setLayoutParams(tvlparams)btn.setLayoutParams(btnlparams) 我会得到:

enter image description here

这是怎么回事?我究竟做错了什么?谢谢。

最佳答案

这应该适合你:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/* create textview */
TextView tv = new TextView(this);
tv.setBackgroundColor(0xff12dd12);
tv.setText("dynamic textview");

/* create button */
Button btn = new Button(this);
btn.setText("dynamic btn");
btn.setBackgroundColor(0xff12dd12);

TableRow.LayoutParams trparams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);
trparams.weight = 3;
tv.setLayoutParams(trparams);
trparams.weight = 2;
trparams.rightMargin = 10;
btn.setLayoutParams(trparams);

TableLayout tableLayout = (TableLayout) findViewById(R.id.layoutTable);

LinearLayout.LayoutParams tableRowParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

/* create a table row */
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableRowParams);

/* add views to the row */
tableRow.addView(tv);
tableRow.addView(btn);

/* add the row to the table */
tableLayout.addView(tableRow);
}

关于java - 无法通过 Java 在 XML 资源文件中复制布局外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49329073/

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