【问题标题】:Having issues with gravity and linear layouts重力和线性布局有问题
【发布时间】:2014-07-14 00:59:49
【问题描述】:

自从我开始 android 开发以来,我在过去 6 个月里经常使用这个网站,还没有提出任何问题,但现在我遇到了一个让我发疯的问题,我无法弄清楚.

基本上我要做的是HORIZONTALLY 创建一个GridView,它占据屏幕的大约 4/10,垂直 LinearLayout 2/10,其中包含一些按钮,最后是一个滚动视图,它将占用大约占屏幕的 4/10。

GridView 4/10th Middle LinearLayout 2/10th scrollview 4/10th

我的问题是布局没有正确显示。我的 GridView 将占据与我的滚动视图相同的一半屏幕,并且我的中间布局将消失。

或者

Gridview 只占(视觉上大约 80px),我中间的LinearLayout 将占据几乎整个屏幕,我的滚动视图将占据大约三分之一。

我在我的 xml 文件中错过了很多重力和其他内容,但没有成功。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity_Test"
    android:orientation="horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" 
    android:paddingTop="5dp"
    android:paddingBottom="5dp" >


    <GridView
        android:id="@+id/image_grid_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:numColumns="@integer/num_columns"
        android:verticalSpacing="2dip"
        android:horizontalSpacing="2dip"
        android:gravity="left"
        android:stretchMode="columnWidth"
        android:layout_weight=".4"
        android:background="@color/grid_background"  />


    <LinearLayout
        android:id="@+id/middle"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".2">

        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home"/>

        <Button
            android:id="@+id/battle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/begin"/>
    </LinearLayout>


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".3">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/t1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

非常感谢大家!

PS:什么是声望点,我如何获得它们?我想附上一张图片让你们了解我的目标,但它说我至少需要其中的 10 个。

【问题讨论】:

  • 把图片链接放在那里,或者在 cmets 里放在这里,我们会帮你放的。
  • 我最终务实地这样做了。我该如何结束这个问题?我无法回答我自己的问题...

标签: android android-layout


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity_Test"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="5dp">


    <GridView
        android:id="@+id/image_grid_view"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:numColumns="@integer/num_columns"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:gravity="left"
        android:stretchMode="columnWidth"
        android:layout_weight=".40"
        android:background="@color/grid_background"  />


    <LinearLayout
        android:id="@+id/middle"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".20">

        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home"/>

        <Button
            android:id="@+id/battle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/begin"/>
    </LinearLayout>


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".40">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/t1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

            <Button
                android:id="@+id/t5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/test_bt_pick_pilot"/>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多