【问题标题】:ImageView on top of Button按钮顶部的 ImageView
【发布时间】:2016-10-04 05:23:51
【问题描述】:

我试图将一个小的 ImageView 放在一个按钮的顶部,但不知何故它一直在它下面。我不明白为什么这种布局与其他视图完美配合。

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/background_white_clickable"
                android:text="@string/book_now"
                android:id="@+id/but_book_now"
                 />

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:id="@+id/img_bookmark"
                android:src="@drawable/ic_action_bookmark_full"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

我尝试过为按钮设置不同的背景,但图像只有在透明时才能看到。我也试过用彩色视图替换 ImageView,还是不行。

有什么想法吗?谢谢

编辑:

我得到的答案很混乱,我的意思是让 ImageView 按 Z 顺序而不是 y 轴在 Button 顶部。

编辑 2:

我不想使用图像按钮,图像只是按钮上会出现/消失的“图钉”。 我不想在按钮上使用drawable,我需要将它放在我想要的位置。 使用 FrameLayout 而不是 RelativeLayout 不会改变任何东西。 使用 imgView.bringToFront() 不起作用。

编辑 3:

在 Lollipop 之前,该问题不会出现在设备上。在 KitKat 上,布局按预期工作。

【问题讨论】:

  • 你需要把图片放在按钮上面吗?
  • 如果你想要一个按钮上的图像,ImageButton 可能是一个更好的主意。无论如何,在相对布局中,z 顺序是由文件中视图的顺序完成的。因此,如果图像是第二个定义的,它应该绘制在按钮的顶部。我们可能难以准确理解您想要什么,您拥有和想要什么的图片是个好主意。
  • @Shadow 是的,在顶部按 z 顺序,而不是 y 轴。
  • 找到了解决方法。将 ImageView 的高度设置为 1dp。检查这个。 stackoverflow.com/questions/28105551/…

标签: java android android-layout


【解决方案1】:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="80dp" >
<Button
    android:id="@+id/filter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="7dp"
    android:background="#0000FF"
    android:gravity="center"
    android:paddingBottom="25dp"
    android:paddingTop="25dp"
    android:text="Sort"
    android:textColor="#FFFFFF" />

<ImageView
android:id="@+id/widget_title_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:adjustViewBounds="true"
android:paddingTop="-10dp"
android:scaleType="fitStart"
android:src="@drawable/ic_launcher" />

    </FrameLayout>

试试吧,虽然我还没有测试过。

【讨论】:

    【解决方案2】:

    试试

    findViewById(R.id.img_bookmark).bringToFront();
    

    【讨论】:

    • 有线。在bringToFront() 之后尝试findViewById(R.id.img_bookmark).invalidate();,如果这也不会帮助使RelativeLayout 无效。
    【解决方案3】:

    你需要在imageview下面设置按钮:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_white_clickable"
            android:text="@string/book_now                     
            android:id="@+id/but_book_now"
            android:layout_below="@+id/img_bookmark"/>
    
        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/img_bookmark"
            android:src="@drawable/ic_action_bookmark_full"
            android:layout_marginRight="15dp"
            android:layout_marginEnd="15dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
    

    编辑

    我现在注意到您想要在按钮的顶部而不是 ABOVE 上的图像,所以不要关心我上面的代码并使用 ImageButton:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_white_clickable"
            android:text="@string/book_now                     
            android:id="@+id/but_book_now"
            android:src="@drawable/ic_action_bookmark_full"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      示例代码:

      <RelativeLayout
              android:id="@+id/relativeLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
      
              <ImageView
                  android:id="@+id/img_bookmark"
                  android:layout_width="35dp"
                  android:layout_height="35dp"
                  android:layout_alignParentRight="true"
                  android:src="@drawable/ic_launcher" />
      
              <Button
                  android:id="@+id/but_book_now"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_toLeftOf="@+id/img_bookmark"
                  android:text="Test" />
          </RelativeLayout>
      

      注意:

      根据您的需要编辑文本和源代码

      【讨论】:

        【解决方案5】:

        如果你想要带按钮的图像视图:

            <Button
                android:id="@+id/but_book_now"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/ic_launcher"
                android:background="#ff00ff"
                android:text="This is button"
                android:padding="10dp"
                android:textColor="#ffff00" 
                />
        
        </RelativeLayout>
        

        如果你想要没有按钮的图像视图:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
        
            <ImageView
                android:id="@+id/but_book_now"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:text="This is button"
                android:padding="10dp"
                android:textColor="#ffff00" 
                />
        
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ff00ff"
                android:text="This is button"
                android:padding="10dp"
                android:textColor="#ffff00" 
                />
        
        </LinearLayout>
        

        【讨论】:

          【解决方案6】:

          您可以将 RelativeLayout 定义为 Button 并在其上添加 ClickEvents,添加带有颜色的子 View 或带有 PNG 的子 ImageView,或两者兼有,以在其上放置图像或颜色。

          例子

                      <FrameLayout android:id="@+id/buttonTextFillColor"
                          android:layout_width="@dimen/mini_icon_width"
                          android:layout_height="@dimen/mini_icon_height"
                          android:background="@drawable/input_button_background"
                          android:clickable="true">
                          <View
                              android:id="@+id/colorTextFillColor"
                              android:layout_width="23dp"
                              android:layout_height="23dp"
                              android:background="#FFFFFF"
                              android:layout_gravity="center" />
                          <ImageView
                              android:layout_width="23dp"
                              android:layout_height="23dp"
                              android:src="@drawable/button_fill_text"
                              android:layout_gravity="center"/>
                      </FrameLayout>
          

          你得到一个元素的联合:

          How will you see it?

          【讨论】:

            【解决方案7】:

            请为Button属性添加layout_below

            【讨论】:

            • 您能否添加有关您提供的解决方案的更多详细信息?
            猜你喜欢
            • 2019-09-20
            • 2013-01-14
            • 2017-10-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-21
            • 2011-03-18
            • 1970-01-01
            相关资源
            最近更新 更多