1

我的问题是我似乎无法让我的菜单布局正常工作。到目前为止,我已经尝试使用 LayoutWeight 来尝试让 ImageButton 使用一定数量的空间,但我似乎无法让它工作,并且前两个和后两个 ImageButton 之间存在很大差距。

下面是一个非常快速的线框链接,我希望布局看起来像这样,下面是我目前拥有的代码,没有底部的 TextView。

我不确定这是否是一个专门的编程问题,但我正在寻找有关布局的有用资源,关于我应该为代码的某些部分查看什么的建议,或者可以做我想做的/非常事情的代码相似的。

任何和所有的帮助表示赞赏,谢谢。

我的快速线框图上的快速注释-

我基本上希望徽标占据垂直像素/屏幕的 10%,方形图像每行占据 35%,因此总体上占 70%。剩下的 20% 分配给底部的 TextView。

图像

    <?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"
    android:background="@drawable/bg_image"
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_hb" 
        />    
    <LinearLayout  android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">


    <ImageView
        android:id="@+id/imageButton1"        
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/imagebutton1"
        android:layout_weight="50"
        />

    <ImageView
        android:id="@+id/imageButton2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:src="@drawable/imagebutton2" 
        android:layout_weight="50"
        />


    </LinearLayout>
<LinearLayout  android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100"
        >
    <ImageView
        android:id="@+id/imageButton3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/imagebutton3"
        android:layout_weight="50" 
        />

    <ImageView
        android:id="@+id/imageButton4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/imagebutton4"
        android:layout_weight="50" 
        />


    </LinearLayout>

</LinearLayout>
4

2 回答 2

0

添加android:weightSum="100"到您的主LinearLayout布局,然后通过指定为其子布局指定所需的权重android:layout_weight="weight_value"

<?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"    
    android:weightSum="100">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    android:layout_weight="10"/>    

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    android:layout_weight="35" >

<ImageView
    android:id="@+id/imageButton1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_weight="50"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageButton2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:layout_weight="50"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" 
    android:layout_weight="35">

<ImageView
    android:id="@+id/imageButton3"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_weight="50"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageButton4"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_weight="50"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:text="TextView" />

</LinearLayout>
于 2012-12-11T15:23:49.987 回答
0

最简单的方法是使用相对布局而不是线性布局。只需将您需要的控件放到设计师那里,然后将它们锚定在正确的位置。唯一的一个问题是强迫你的正方形 vievs 正方形。可能您必须制作自己的控件并覆盖它的 onMeasure() 方法以设置高度等于宽度。

于 2012-12-11T15:51:57.443 回答