2013-10-18 43 views
0

在我的应用程序中我有一个显示数据表的活动。TableLayout中的宽度

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

使用下面的代码在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> 

它是在的LinearLayout使用id =画报其中I在适当类绘制表作为如下:

@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中任何东西。 ly me。

我希望你能帮助我,这是我的第三篇文章,有同样的信息,任何人都可以回复我。

谢谢。

回答

1

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

滚动型scvista =新滚动型(本); 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));

试试这个亲爱的:)

+0

此表格不起作用! – KryNaC

+0

如果我只是使用表或垂直滚动​​与表,如果它的工作和占用全宽,但不能使用horizo​​ntall滚动... – KryNaC

相关问题