【问题标题】:How to get my weight in linear layout to properly align the buttons?如何在线性布局中获得我的重量以正确对齐按钮?
【发布时间】:2012-08-22 18:52:57
【问题描述】:

我正在尝试配置我的按钮布局,但无法正确形成结构。当我在 VM 上运行代码时,它使 ImageButton 广告占据了屏幕的 50% 以上,尽管重量仅为 10%。每个图像按钮引用一个存储在drawable-ldpi中的图像,每个大小大约为1 KB,并且活动代码是基本的,没有添加,这目前是通过主xml的纯图形界面。

预期布局:

 _______________________
|        logo          |
|______________________|
|       search         |
|______________________|
|                      |
|     featured         |
|                      |
|______________________|
|    |     |     |     |
|    |     |     |     |
|    |     |     |     |
|    |     |     |     |
|    |     |     |     |
|    |     |     |     |
|    |     |     |     |
|____|_____|_____|_____|
|     advertisement    |
|______________________|

安卓 MAIN.XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/toplayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="45"
        android:gravity="top"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/mytastelogo"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="15"
            android:src="@drawable/mytastelogo_image" />

        <ImageButton
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="10" />

        <ImageButton
            android:id="@+id/featuredpanel"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="20" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomlayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="45"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/mycollection"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/mycollection_image" />

        <ImageButton
            android:id="@+id/myrecommendations"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/myrecommendations_image" />

        <ImageButton
            android:id="@+id/topitems"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/topitems_image" />

        <ImageButton
            android:id="@+id/randomitem"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/randomitem_image" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/adlayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:gravity="end" >

        <ImageButton
            android:id="@+id/advertisement"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android button imagebutton


    【解决方案1】:

    试试吧 :-)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/toplayout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="45"
        android:gravity="top"
        android:orientation="vertical" >
    
        <ImageButton
            android:id="@+id/mytastelogo"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="15" />
    
        <ImageButton
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="10" />
    
        <ImageButton
            android:id="@+id/featuredpanel"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="20" />
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/bottomlayout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="45"
        android:gravity="bottom"
        android:orientation="horizontal" >
    
        <ImageButton
            android:id="@+id/mycollection"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    
        <ImageButton
            android:id="@+id/myrecommendations"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    
        <ImageButton
            android:id="@+id/topitems"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    
        <ImageButton
            android:id="@+id/randomitem"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/adlayout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="10" >
    
        <ImageButton
            android:id="@+id/advertisement"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      当您的方向为vertical 时,您需要将layout_height 设置为0dp。当它是horizontal 时,将您的layout_width 设置为0dp

      例如:

      <LinearLayout
              android:id="@+id/toplayout"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="45"
              android:gravity="top"
              android:orientation="vertical" >
      

      【讨论】:

      • 这使得按钮完全消失,而其他按钮仍然卡在顶部。
      • 当你的LinearLayout方向是水平的时候,你的layout_width应该是0dp,当它是垂直的时候,你的layout_height应该是0dp。
      • 是的,这就是我在上一个问题中最初使用代码的方式,修复该问题的答案是将 0dp 更改为 match_parent。现在,当我切换回来时,我又回到了按钮根本没有出现的问题上。
      【解决方案3】:

      所有重量决定其高度的项目都应将 android:layout_height 设置为 0dp。

      android:layout_height="0dp"
      

      同样对于宽度由重量决定的物品,你必须设置

      android:layout_width="0dp"
      

      【讨论】:

      • 这导致布局中的按钮集完全消失。
      • 如果您希望高度由重量决定,您可以将高度设为 0dp 并指定重量。如果你想按重量分配宽度,你可以为 width 属性做,但保持高度为正常值。
      猜你喜欢
      • 2016-09-26
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 2018-06-18
      • 2014-01-14
      相关资源
      最近更新 更多