2012-08-09 166 views
0

我试图用一些线性布局来构建网格。 不幸的是,“网格”内的按钮填充所有空间,“网格”并不总是相同的大小。带有LinearLayout的Android网格

<ScrollView 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_weight="1"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/banner" 
    android:background="#80000000" 
    android:contentDescription="@string/about"/> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginTop="15dp" 
    android:gravity="center_horizontal" 
    android:text="@string/modi1" 
    tools:context=".MenuActivity" /> 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_marginTop="15dp" 
    android:gravity="center_horizontal" 
    android:text="@string/modi2" 
    tools:context=".MenuActivity" /> 
    </LinearLayout> 

    <LinearLayout  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/szen_diagramm" /> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center"   
    android:text="@string/szen_countdown" /> 
    </LinearLayout> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/szen_diagramm" /> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center"   
    android:text="@string/szen_countdown" /> 
    </LinearLayout> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/szen_diagramm" /> 
    <Button 
    android:layout_marginTop="10dp" 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center"   
    android:text="@string/szen_countdown" /> 
    </LinearLayout> 
</LinearLayout> 
</ScrollView> 

它应该是这个样子 this 与中心的按钮,并在他们里面的文字需要的大小。

任何人都可以帮助我吗?

+0

你想用'LinearLayout'构建一个网格?为什么?使用'TableLayout'来代替。这就是它存在的原因! – yugidroid 2012-08-09 23:53:42

回答

4

试试这个。

<ScrollView xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TableRow> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#80000000" 
      android:contentDescription="ImageView" 
      android:layout_span="2" 
      android:src="@drawable/banner" /> 
    </TableRow> 

    <TableRow> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" 
      android:layout_weight="2" 
      android:gravity="center_horizontal" 
      android:text="modi1" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" 
      android:layout_weight="2" 
      android:gravity="center_horizontal" 
      android:text="modi2" /> 
    </TableRow> 

    <TableRow> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_diagramm" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_countdown" /> 
    </TableRow> 

    <TableRow> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_diagramm" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_countdown" /> 
    </TableRow> 

    <TableRow> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_diagramm" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="szen_countdown" /> 
    </TableRow> 
</TableLayout> 

enter image description here 这是怎么显示我

+0

嗨,谢谢,我用这个代码,但按钮仍然是全宽和垂直不正确:([截图](https://www.dropbox.com/s/g42dpuyzvad9k09/Screenshot_2012-08-09-13- 17-24.png) – Philipp 2012-08-09 11:22:09

+0

你可以解释你想要按钮的确切位置吗? – Beenal 2012-08-09 11:56:42

+0

就像一个网格......水平相同的高度和按钮宽度与buttontext相同,而不是整个网格如果你需要进一步解释我可以画出你的照片,但我现在在工作吗?所以这有点困难......在你的例子中:如果你将buttontext更改为更短的“abc”,那么buttonwidth仍然保持不变,如果我改变它更长的像“abc abc abc abc abc abc abc”这些按钮不与另一侧水平居中 – Philipp 2012-08-09 12:05:28

1

删除android:layout_weight =“1”以避免按钮填满所有空间。

你应该尝试使用TableLayout和几个的TableRow来替换包含的按钮进行LinearLayouts,从而创造一个良好格式化网格

+0

嗨,感谢您的回复。所有的layout_weights,或只是在表格,表格行,textview或按钮?无法弄清楚 - – Philipp 2012-08-09 11:23:50

+0

TextView的重量应该足够了。 您应该使用Eclipse的Android布局的可视化编辑器,并使用不同的参数进行快速测试。 – Laucia 2012-08-09 11:41:40

0

我有一个类似的问题,解决了在插入一个的TableRow的LinearLayout。查看发布的xml代码并验证它是否符合您的要求。

<TableRow 
    android:layout_gravity="fill_horizontal" 
    android:paddingTop="@dimen/table_row_padding_top" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:gravity="center_horizontal" 
      android:orientation="vertical" > 

      <ImageButton 
       android:contentDescription="@string/str_phone" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:background="@drawable/icon_phone" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_horizontal" 
       android:text="@string/str_phone" 
       android:textColor="#000000" 
       android:textSize="12sp" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:gravity="center_horizontal" 
      android:orientation="vertical" > 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:background="@drawable/icon_car" 
       android:contentDescription="@string/str_car" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_horizontal" 
       android:text="@string/str_car" 
       android:textColor="#000000" 
       android:textSize="12sp" /> 
     </LinearLayout> 

</TableRow>