在我的应用程序中,我有一个显示数据表的活动。
问题是我无法将表格的宽度调整到屏幕的整个宽度。在智能手机中,桌子的宽度大于设备的宽度,所以我使用滚动,而在平板电脑中,桌子的宽度小于它的宽度,所以我想适应总宽度。
在 xml 中使用以下代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/fondo_pantalla" >
<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:text="@string/city_Vld"
android:textColor="@color/blanco"
android:gravity="center"
android:background="@color/color_Vld" />
<TextView
android:id="@+id/Title_O"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Title"
android:textColor="@color/color_Vld"
android:gravity="center"
android:background="@color/blanco" />
<LinearLayout
android:id="@+id/grafico"
android:layout_width="fill_parent"
android:layout_heightt="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@color/black"
android:orientation="vertical"
android:layout_below="@+id/Title_O" />
</RelativeLayout>
它在id = grafico的线性布局中,我在适当的类中绘制表如下:
@SuppressWarnings("deprecation")
private void pintaTexto(){
ScrollView scvista = new ScrollView(this);
scvista.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
HorizontalScrollView hscvista = new HorizontalScrollView(this);
hscvista.setLayoutParams(new HorizontalScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tabla = new TableLayout(this);
tabla.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
double numRows = 20;
int numColumns = 4;
// Header of the table
TableRow cabecera = new TableRow(this);
cabecera.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
cabecera.setBackgroundColor(getResources().getColor(R.color.azulOscuro_elvg));
tabla.addView(cabecera);
// Header data
for (int j = 0; j < numColumns; j++) {
TextView textColumna = new TextView(this);
textColumna.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textColumna.setText(json.json4.GetInfo(0, j));
textColumna.setTextColor(getResources().getColor(R.color.blanco));
textColumna.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textColumna.setGravity(Gravity.CENTER_HORIZONTAL);
textColumna.setPadding(5, 5, 5, 5);
cabecera.addView(textColumna);
}
// Data row
for (int f = 0; f < numRows; f++) {
TableRow row = new TableRow(this);
for (int c = 0; c < numColumns; c++) {
TextView textDato = new TextView(this);
textDato.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 60));
textDato.setText(json.json4.GetValue(f, c));
textDato.setTextColor(getResources().getColor(R.color.azulOscuro_elvg));
textDato.setBackgroundColor(getResources().getColor(R.color.blanco));
textDato.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
textDato.setGravity(Gravity.CENTER_VERTICAL);
textDato.setPadding(15, 0, 15, 0);
row.addView(textDato);
}
tabla.addView(fila);
}
hscvista.addView(tabla);
scvista.addView(hscvista);
LinearLayout layout = (LinearLayout) findViewById(R.id.grafico);
layout.addView(scvista);
}
我已经检查了所有元素的宽度属性,但我无法像我指出的那样适合FILL_PARENT。此外,这个带有id = grafico的LinearLayout也用于绘制条形图,如果适合整个屏幕,所以我认为它不能在xml中有任何内容。是我。
我希望你能帮助我,这是我的第三篇帖子,有相同的信息,有人回复我任何事情。
谢谢。
为滚动视图宽度和高度填充父项
ScrollViewscvista = new ScrollView(this);scvista.setLayoutParams(new LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent));HorizontalScrollView hscvista = new HorizontalScrollView(this);hscvista.setLayoutParams(new HorizontalScrollView.LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent));TableLayout tabla = new TableLayout(this);tabla.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
试试这个亲爱的:)