【问题标题】:ImageButton not Acting as expected in LinearLayoutImageButton 在 LinearLayout 中未按预期运行
【发布时间】:2013-10-05 18:20:47
【问题描述】:

下面是代码:

     <LinearLayout
            android:id="@+id/llReviewView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#d8d9d9"
            android:orientation="vertical" >

    <LinearLayout
            android:id="@id/header"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:background="#fff"
            android:clickable="false"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/bWiki"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="90"
                android:background="@drawable/wiki_selector"
                android:contentDescription="Wiki Link"
                android:src="@drawable/ic_info_wiki" />

            <LinearLayout
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#666" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/spinner_bg_selector"
                android:orientation="vertical" >

                <Spinner
                    android:id="@+id/spinnerSort"
                    android:layout_width="100dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/spinnerselector" />
            </LinearLayout>
        </LinearLayout>
     // edited
 </LinearLayout>

wiki_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Pressed -->
    <item android:drawable="@drawable/row_press" android:state_pressed="true"/>

     <!-- Normal -->
    <item android:drawable="@drawable/wiki_normal" android:state_pressed="false"/>


</selector>

这是视图的图像:

问题如下:

  1. 只有按图片按钮中间的“i”图标才有效。
  2. 单击空白处不会执行任何操作,但会激活选择器 - 右侧 Spinner 的选择器!
  3. wiki_selector 什么都不做。

【问题讨论】:

  • 您尚未复制问题中的完整布局。
  • 如果您的父 ViewGroup 的方向为 android:orientation="vertical",则图片中不会出现上述布局。您可能也需要更新它。

标签: android android-linearlayout imagebutton


【解决方案1】:

“i”按钮仅在其边界矩形内点击时才会响应。我想,既然你有 width=0dp,它就会被设置为它的最小宽度。

您需要确定微调器与“i”按钮所占的比例。另外,我不确定您对这两个 LinearLayout 有什么想法。如果它已经显示了您想要的所有内容,那么您就不会使用 LinearLayouts。

现在,假设您希望微调器仅占据其内容所需的宽度。然后,您应该将其宽度设置为 WRAP_CONTENT。对于微调器,您可以选择 MATCH_PARENT 并(我不确定)添加 layout_weight="1"。

或者,如果您希望“i”按钮占据 2/3 的宽度和微调器的 1/3,则您需要宽度都为“0dp”,并且“i”按钮应该具有 layout_weight="2 ", 微调器 "1"。

【讨论】:

  • 如果这个答案有帮助,请点赞。如果您认为这是正确的答案,请检查它。这将鼓励人们回答未来的问题:)
  • 抱歉耽搁了这么久。虽然我标记为正确,但我没有。这有帮助,谢谢!
猜你喜欢
  • 2018-08-05
  • 2019-09-07
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 2020-06-28
  • 2012-02-18
  • 2018-01-18
  • 2012-06-14
相关资源
最近更新 更多