Android (Java):以编程方式将权重为 1 的 TextView 添加到表格行不会导致等宽的 TextView
Android (Java): Programatically adding TextView(s) with weight 1 to a TableRow does not result in TextView(s) of equal width
在我的 Android 应用程序中,我试图以编程方式将 TableRow
添加到 TableLayout
,其中 TableRow
具有 TextView
(s)重量相等 (1),这意味着它们占据相同的宽度。下图显示了我正在努力实现的目标(我正在尝试以编程方式添加以红色突出显示的 TableRow
):
但是,下面是我的代码在以编程方式添加具有 3 个 TextView
(s) 等权重 1 的 TableRow 之后的结果:
下面是主布局的 XML,以及用于以编程方式创建权重 1 的 TextView
的模板。考虑到用于以编程方式创建权重 1 的模板,错误看起来很奇怪TextView
,我指定权重为1.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TableRow
android:background="#d6d7d7"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</LinearLayout>
table_textview.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000" />
MainActivity.java:
package com.example.programaticallyaddtablerowtotablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve of the table layout.
TableLayout table = (TableLayout) findViewById(R.id.table_layout);
// Initialize the row to be added to the table layout.
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// Add the TextView(s) to the row.
TextView xView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
xView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
xView.setText("X");
newRow.addView(xView);
TextView yView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
yView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
yView.setText("Y");
newRow.addView(yView);
TextView zView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
zView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
zView.setText("Z");
newRow.addView(zView);
// Add the row to the table layout.
table.addView(newRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
// Creates a View by inflating a template XML layout.
private View createViewFromXMLTemplate(int layout) {
return getLayoutInflater().inflate(layout, null);
}
}
你必须像下面那样做才能在 TextView
上获得 weight
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
尝试使用 setColumnStretchable (int columnIndex, boolean isStretchable)
在我的 Android 应用程序中,我试图以编程方式将 TableRow
添加到 TableLayout
,其中 TableRow
具有 TextView
(s)重量相等 (1),这意味着它们占据相同的宽度。下图显示了我正在努力实现的目标(我正在尝试以编程方式添加以红色突出显示的 TableRow
):
但是,下面是我的代码在以编程方式添加具有 3 个 TextView
(s) 等权重 1 的 TableRow 之后的结果:
下面是主布局的 XML,以及用于以编程方式创建权重 1 的 TextView
的模板。考虑到用于以编程方式创建权重 1 的模板,错误看起来很奇怪TextView
,我指定权重为1.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TableRow
android:background="#d6d7d7"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</LinearLayout>
table_textview.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000" />
MainActivity.java:
package com.example.programaticallyaddtablerowtotablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve of the table layout.
TableLayout table = (TableLayout) findViewById(R.id.table_layout);
// Initialize the row to be added to the table layout.
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// Add the TextView(s) to the row.
TextView xView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
xView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
xView.setText("X");
newRow.addView(xView);
TextView yView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
yView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
yView.setText("Y");
newRow.addView(yView);
TextView zView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
zView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
zView.setText("Z");
newRow.addView(zView);
// Add the row to the table layout.
table.addView(newRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
// Creates a View by inflating a template XML layout.
private View createViewFromXMLTemplate(int layout) {
return getLayoutInflater().inflate(layout, null);
}
}
你必须像下面那样做才能在 TextView
上获得weight
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
尝试使用 setColumnStretchable (int columnIndex, boolean isStretchable)