通过XML为Android应用程序创建主菜单

时间:2013-12-26 21:37:41

标签: java android xml eclipse

我是Android新手(今年才开始在大学学习),我正在尝试通过XML为我的应用设置一个主菜单。

基本上我想要的是我的四个按钮周围有20%的边框。

以下代码是我到目前为止所用的,它似乎工作正常,但我不确定我是否以正确的方式完成了它? Eclipse目前给了我两个警告,一个用于缩进的LinearLayout,说它可能没用,另一个用于嵌套权重,表示性能不佳。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_weight="8"
        android:weightSum="10" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="2"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="2"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="2"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="2"/>
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

你的布局非常好,eclipse给你关于布局没有用的警告,只是因为你有两个父布局,但你需要它们是2来实现你想要的,通过给予weightSum = 10的主要父布局,权重= 8到chid,在边界的20%(不是80%,但它很容易操纵)