组合文本和图像按钮,调整可绘制的大小

时间:2016-07-01 11:31:24

标签: android xml android-layout resize drawable

我有一个按钮,我在我的Android项目中的一个活动中实现了 - 按钮应该包含一个徽标(一个可绘制的)和一些跟随它的文本,但我有一个非常特殊的问题。在我的活动xml文件的预览视图中,按钮看起来非常好,但是当我在手机上运行我的应用程序时,它变得非常大。

我从我自己制作的.xml drawable中导入drawable。我尝试将.png直接导入到我的活动中,但它太大了 - 所以我创建了一个新的xml,我调整了它, xml是我导入的可绘制的。代码:

Activity.xml

EAST

fb_resize.xml(我的可绘制)

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/title_activity_facebookInvite"
    android:id="@+id/btnInviteFacebook"
    android:layout_alignParentBottom="false"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/mainActivityInviteText"
    android:background="@drawable/bh_red_button"
    android:layout_marginTop="@dimen/bh_input_element_margin"
    android:textSize="@dimen/bh_button_font_size"
    android:drawableLeft="@drawable/fb_resize"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:minHeight="0dp" />

fb_logo是我做的PNG。另一个非常奇怪的事情是,当我在Android Studio中的虚拟设备上模拟它时,它看起来非常好,但不在我的手机上(尝试过不同的手机)。

2 个答案:

答案 0 :(得分:1)

看起来这是你的问题:

android:layout_width="fill_parent"

尝试将fill_parent替换为wrap_content

你也可以尝试在你的项目中添加它:

 android:scaleType="fitCenter"
 android:adjustViewBounds="true"

好的,这是一种解决方法,请尝试用以下方法替换您的按钮:

<FrameLayout
    android:id="@+id/NavigateRightButtonLayout"          
    android:layout_below="@+id/mainActivityInviteText"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:gravity="center">
    <Button
     android:textSize="@dimen/bh_button_font_size"
     android:textColor="#FFFFFF"
     android:textAlignment="gravity"
     android:text="@string/title_activity_facebookInvite"
     android:gravity="center"
     android:background="@drawable/bh_red_button"
     android:layout_marginTop="@dimen/bh_input_element_margin"
     android:textSize="@dimen/bh_button_font_size"
     android:layout_gravity="center"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
      <ImageView
       android:layout_width="15dp"
       android:layout_height="15dp"
       android:layout_gravity="left"
       android:id="@+id/fb_resizeBtn"
       android:background="@mipmap/fb_logo" />
</FrameLayout>

答案 1 :(得分:1)

解决了!而不是.png,我使用了矢量图片(.svg文件),并且出于某种原因调整了大小。