组合文本和图像按钮,调整可绘制大小
Combined text- and image-button, resizing drawable
我在 Android 项目的一项活动中实现了一个按钮 - 该按钮应该包含徽标(可绘制对象)和后面的一些文本,但我有一个非常奇特的问题。在我的 activity xml 文件的预览视图中,该按钮看起来非常好,但是当我 运行 我的应用程序在我的 phone 上时,它变得非常大。
我从我自己制作的 .xml 可绘制对象中导入可绘制对象。我尝试将 .png 直接导入我的 activity 但它太大了 - 所以我制作了一个新的 xml 并调整了它的大小 that xml 是我导入的可绘制对象。代码:
Activity.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_resize.xml(我的可绘制对象)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fb_resizeBtn"
android:drawable="@mipmap/fb_logo"
android:width="15dp"
android:height="15dp"
/>
</layer-list>
fb_logo 是我制作的 PNG。另一个非常奇怪的事情是,当我在 Android Studio 中的虚拟设备上模拟它时,它看起来非常好,但在我的 phone 上却不是(也尝试过不同的 phone)。
看来这是你的问题:
android:layout_width="fill_parent"
尝试用 wrap_content
替换 fill_parent
您也可以尝试将此添加到您的项目中:
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>
解决了!我没有使用 .png,而是使用了矢量图片(.svg 文件),并且出于某种原因调整了大小。
我在 Android 项目的一项活动中实现了一个按钮 - 该按钮应该包含徽标(可绘制对象)和后面的一些文本,但我有一个非常奇特的问题。在我的 activity xml 文件的预览视图中,该按钮看起来非常好,但是当我 运行 我的应用程序在我的 phone 上时,它变得非常大。
我从我自己制作的 .xml 可绘制对象中导入可绘制对象。我尝试将 .png 直接导入我的 activity 但它太大了 - 所以我制作了一个新的 xml 并调整了它的大小 that xml 是我导入的可绘制对象。代码:
Activity.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_resize.xml(我的可绘制对象)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fb_resizeBtn"
android:drawable="@mipmap/fb_logo"
android:width="15dp"
android:height="15dp"
/>
</layer-list>
fb_logo 是我制作的 PNG。另一个非常奇怪的事情是,当我在 Android Studio 中的虚拟设备上模拟它时,它看起来非常好,但在我的 phone 上却不是(也尝试过不同的 phone)。
看来这是你的问题:
android:layout_width="fill_parent"
尝试用 wrap_content
fill_parent
您也可以尝试将此添加到您的项目中:
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>
解决了!我没有使用 .png,而是使用了矢量图片(.svg 文件),并且出于某种原因调整了大小。