0

在我的应用程序中,我有一个显示包含数据的表格的活动。

问题是我无法将表格的宽度调整为屏幕的全宽。在智能手机中,表格的宽度大于设备的宽度,所以我使用滚动条,而在平板电脑中,表格的宽度小于它的宽度,所以我想适应总宽度。

在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 的 LinearLayout 中,我在适当的中绘制表格,如下所示:

@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 中进行任何操作。我。

我希望你能帮助我,这是我第三次发布同样的信息,任何人都回复我。

谢谢。

4

1 回答 1

1

给滚动视图宽度和高度填充父级

ScrollView scvista = new ScrollView(this); scvista.setLayoutParams(new LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent)); Horizo​​ntalScrollView hscvista = new Horizo​​ntalScrollView(this); hscvista.setLayoutParams(new Horizo​​ntalScrollView.LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent)); TableLayout tabla = new TableLayout(this); tabla.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

试试这个亲爱的:)

于 2013-10-18T12:44:00.753 回答