Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带线性布局的Android网格_Android_Xml_Eclipse_Android Linearlayout - Fatal编程技术网

带线性布局的Android网格

带线性布局的Android网格,android,xml,eclipse,android-linearlayout,Android,Xml,Eclipse,Android Linearlayout,我正在尝试用一些线性布局构建一个网格。 不幸的是,“网格”内的按钮填满了所有空间,“网格”的大小并不总是相同的 <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"

我正在尝试用一些线性布局构建一个网格。 不幸的是,“网格”内的按钮填满了所有空间,“网格”的大小并不总是相同的

<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>

它应该看起来像 使按钮居中,并达到所需的大小,且文本位于内部


有人能帮我吗?

删除android:layout\u weight=“1”以避免按钮填满所有空间


您应该尝试使用一个TableLayout和几个TableRow来替换包含按钮的线性布局,从而创建一个格式良好的网格

删除android:layout\u weight=“1”以避免按钮填满所有空间

您应该尝试使用一个TableLayout和几个TableRow来替换包含按钮的线性布局,从而创建一个格式良好的网格

<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>

这就是它向我展示的方式

试试这个

<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>


这就是它如何向我显示的

我遇到了类似的问题,解决了在TableRow中插入线性布局的问题。查看发布的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>

我遇到了类似的问题,解决了在TableRow中插入线性布局的问题。查看发布的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>


您好,谢谢,我使用了此代码,但按钮仍然是全宽的,垂直方向不正确:(你能解释一下你想要按钮的确切程度吗?就像一个网格…水平高度相同,按钮宽度与buttontext一样宽,而不是整个网格。如果你需要任何进一步的解释,我可以给你画一张图,但我现在正在工作?所以这有点困难…在你的例子中:如果你把buttontext改为短一些,比如“abc”按钮宽度仍然保持不变,如果我将其更改为“abc”等更长的按钮,则按钮不会水平居中,而另一侧则可以使用布局\u width=“wrap\u内容“,对于布局,您是说它应该像网格一样具有相同的水平宽度,但其中的按钮应该是文本的宽度。您找到您要寻找的解决方案了吗?您好,谢谢,我使用了此代码,但按钮仍然是全宽的,垂直方向不正确:(你能解释一下你想要按钮的确切程度吗?就像一个网格…水平高度相同,按钮宽度与buttontext一样宽,而不是整个网格。如果你需要任何进一步的解释,我可以给你画一张图,但我现在正在工作?所以这有点困难…在你的例子中:如果你把buttontext改为短一些,比如“abc”按钮宽度仍然保持不变,如果我将其更改为“abc”等更长的按钮,则按钮不会水平居中,而另一侧则可以使用布局\u width=“wrap\u内容",对于布局,您是说它应该像网格一样具有相同的水平宽度,但其中的按钮应该是文本的宽度。您找到您要寻找的解决方案了吗?您好,谢谢您的回复。所有布局的权重,或者只是在表、表行、文本视图或按钮中?无法计算出来--文本视图的权重应该是足够了。你应该使用Eclipse Android版面的可视化编辑器,并使用不同的参数进行快速测试。嗨,谢谢你的回复。所有版面的权重,或者只是在表格、表格行、textview或按钮中?无法计算-。-textview权重应该足够了。你应该使用Ec Android版面的可视化编辑器lipse并使用不同的参数进行快速测试。你想用
线性布局构建网格
?为什么?用
表格布局
代替。这就是它存在的原因!你想用
线性布局
构建网格?为什么?用
表格布局
代替。这就是它存在的原因!