【问题标题】:button with text and outside of border's image带有文本和边框图像之外的按钮
【发布时间】:2013-08-11 20:37:14
【问题描述】:

我想创建一个Button,让它的一部分图标超出它,像这样:

但是,使用这种 xml 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:descendantFocusability="afterDescendants"
    android:gravity="right"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textSize="40sp" 
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"  />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/btn_background" >

        <Button
            android:id="@+id/delete_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/delete_btn"
            android:drawableRight="@drawable/delete"
            android:text="delete" />

        <Button
            android:id="@+id/button3"
            android:background="@drawable/delete_btn"
            android:drawableLeft="@drawable/share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="share" />

        <Button
            android:id="@+id/like_btn"
            android:background="@drawable/delete_btn"
            android:drawableRight="@drawable/like"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginRight="15dp"
            android:layout_toLeftOf="@+id/delete_btn"
            android:text="like" />

    </RelativeLayout>

</LinearLayout>

我得到这样的东西:

Button 的图像是否可能越界?

谢谢

更新:

删除_btn.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <stroke android:width="1dp" android:color="#F2F2F2" /> 
  <padding android:left="7dp" android:top="1dp" 
    android:right="7dp" android:bottom="1dp" /> 
  <corners android:radius="34dp" /> 
  <solid android:color="#F2F2F2"/> 
</shape>

【问题讨论】:

  • 你可以在上面的代码中设置宽度和高度试试
  • 看看我的回答。像魅力一样工作
  • 创建透明顶部的图像,并将其放在您的按钮上

标签: android image button


【解决方案1】:

请尝试这种方式。

<?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="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@android:color/darker_gray" >

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

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

         <ImageView
            android:id="@+id/iv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

         <ImageView
            android:id="@+id/iv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

您可以自定义 your way

【讨论】:

    【解决方案2】:

    你可能不得不像这样设计你的布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:descendantFocusability="afterDescendants"
    android:gravity="right"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textSize="40sp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"  />
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40sp"
        android:layout_gravity="center">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5sp"
            android:layout_marginBottom="5sp"
            android:background="@drawable/delete_btn" />
    
        <Button
            android:id="@+id/delete_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent"
            android:drawableRight="@drawable/delete"
            android:text="delete" />
    
        <Button
            android:id="@+id/button3"
            android:background="@android:color/transparent"
            android:drawableLeft="@drawable/share"
            android:layout_marginTop="-5sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="share" />
    
        <Button
            android:id="@+id/like_btn"
            android:background="@android:color/transparent"
            android:drawableRight="@drawable/like"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginRight="15dp"
            android:layout_toLeftOf="@+id/delete_btn"
            android:text="like" />
    
    </RelativeLayout>
    

    【讨论】:

    • 谢谢,能举个例子吗?
    • 如果您可以上传可绘制资源文件,我实际上可以向您发送您需要实现的正确代码。
    • 我把它放在第一个帖子
    • 请实现这个代码,看看你是否得到了你想要的视图类型。
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2012-09-03
    • 2011-10-11
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多