2016-02-06 25 views
-1

我在按钮单击事件中添加了一个表格行。我的行有2个textview和2个spinners和1个​​EditText。我希望列的大小相同,但是微调控件的大小会根据所选值更改。请帮助,并分享一些灯。如何保持表列的大小与安卓视图无关

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    <TableLayout 

     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingTop="5dp" 
     android:stretchColumns="*" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TableRow 

      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp"> 

      <TextView 

       android:layout_width="1dp" 
       android:layout_height="fill_parent" 
       android:layout_weight=".2" 
       android:background="@drawable/cell_shape" 
       android:padding="5dp" 
       android:text="S.No" 
       android:singleLine="false" 
       android:textAppearance="?android:attr/textAppearanceMedium"></TextView> 

      <TextView 

       android:layout_width="1dp" 
       android:layout_height="fill_parent" 
       android:layout_weight=".2" 
       android:background="@drawable/cell_shape" 
       android:padding="5dp" 
       android:text="Product" 
       android:textAppearance="?android:attr/textAppearanceMedium"></TextView> 

      <TextView 

       android:layout_width="1dp" 
       android:layout_height="fill_parent" 
       android:background="@drawable/cell_shape" 
       android:layout_weight=".2" 
       android:padding="5dp" 
       android:text="Product List" 
       android:textAppearance="?android:attr/textAppearanceMedium"></TextView> 


     <TextView 

      android:layout_width="1dp" 
      android:layout_height="fill_parent" 
      android:background="@drawable/cell_shape" 
      android:layout_weight=".2" 
      android:padding="5dp" 
      android:text="Price" 
      android:textAppearance="?android:attr/textAppearanceMedium"></TextView> 
     <TextView 

      android:layout_width="1dp" 
      android:layout_height="fill_parent" 
      android:background="@drawable/cell_shape" 
      android:layout_weight=".2" 
      android:padding="5dp" 
      android:text="Quantity" 
      android:textAppearance="?android:attr/textAppearanceMedium"></TextView> 
     </TableRow> 
     <!--<TableRow 
      android:id="@+id/tr1" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp"/>--> 
    </TableLayout> 

    <TableLayout 
android:id="@+id/tlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 

     </TableLayout> 

</LinearLayout> 

    <Button 
     android:id="@+id/addButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Add Item" 
     /> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"><Button 
     android:id="@+id/confirm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 

     /></LinearLayout> 
    </LinearLayout> 

在此先感谢

+0

你可以添加布局的XML ?,它会更容易帮助你。 – kevinkl3

+0

我已经添加了xml布局 –

+0

您是否以编程方式添加新行?如果是这种情况,你是否使用布局和LayoutInflater? – kevinkl3

回答

0

如果微调增长到选定值相匹配你极有可能不得不layout_width设置为wrap_content

这听起来像你应该用更多的东西一样layout_weight正确使用up水平空间

编辑︰ 我看不到这个视图将滚动,如果它变得太大。这听起来像你应该使用RecyclerViewListView而不是你的TableLayout。 列表中的每个项目可以是这样的:

<LinearLayout 
    android:layout_height="?android:attr/listItemPreferredHeight" 
    android:layout_width="match_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/some_text" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:layout_weight="1"> 
    <TextView 
     android:id="@+id/some_more_text" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:layout_weight="1"> 
</LinearLayout> 

注:布局注销我的头顶,可能不是完全有效的:P

你可以明显地把匹配的控制和权重是什么你想,但从你的问题,我假设这是你所寻求的预期行为

+0

随着wrap_content旋转器增长到选定的项目的大小,与layout_weight我没有得到预期的行为 –

+0

我编辑我的答案解决新信息,希望它可以帮助 – keith

+0

感谢基思以上建议,我会尝试列表视图和更新 –