android ImageButton缩放并保持纵横比
我有一个正在尝试布局的屏幕...
基本上,我正在尝试在屏幕上垂直均匀分布 4 个 ImageButton 对象...我使用了这个 此处 均匀分布项目,但现在很难缩放图像,但是保持纵横比...如果我使用 scaleType="centerInside"
它们不会缩放,如果我使用“fitXY”,它们不会保持纵横比...布局如下:
这是代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_margin="0dp"
android:padding="0dp"
android:orientation="vertical"
android:weightSum="5"
>
<ImageButton android:id="@+id/share_song"
android:layout_width="fill_parent" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="15dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="left"
android:src="@drawable/share_song_button_sel"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitXY"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:text="" android:id="@+id/tag_a_song"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="right"
android:layout_gravity="right"
android:background="@drawable/song_check_in_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button android:id="@+id/match_button"
android:layout_width="wrap_content" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="left"
android:background="@drawable/music_match_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button android:id="@+id/friends_button"
android:layout_width="wrap_content" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="right"
android:layout_gravity="right"
android:background="@drawable/my_friends_music_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:padding="0dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="horizontal"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:padding="0dp"
android:layout_gravity="bottom"
>
<ImageView android:src="@drawable/trending_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
希望有人可以提供帮助。
I have a screen that I'm trying to layout...
Basically, I'm trying to evenly distribute 4 ImageButton objects vertically on a screen... I used this here to evenly distribute the items, but now am having a terrible time getting the images to scale but maintain aspect ratio... if I use scaleType="centerInside"
they don't scale, if I use "fitXY" they don't maintain aspect ratio... here is what the layout looks like:
and here is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_margin="0dp"
android:padding="0dp"
android:orientation="vertical"
android:weightSum="5"
>
<ImageButton android:id="@+id/share_song"
android:layout_width="fill_parent" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="15dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="left"
android:src="@drawable/share_song_button_sel"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitXY"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:text="" android:id="@+id/tag_a_song"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="right"
android:layout_gravity="right"
android:background="@drawable/song_check_in_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button android:id="@+id/match_button"
android:layout_width="wrap_content" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="left"
android:background="@drawable/music_match_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button android:id="@+id/friends_button"
android:layout_width="wrap_content" android:text=""
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:gravity="right"
android:layout_gravity="right"
android:background="@drawable/my_friends_music_button_sel"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:padding="0dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="horizontal"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:padding="0dp"
android:layout_gravity="bottom"
>
<ImageView android:src="@drawable/trending_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Hopefully someone can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 ImageButtons 的宽度设置为
fill_parent
并对紧邻左边距的图像使用scaletypefitStart
,对右边距的图像使用fitEnd
。至少就您的示例图像而言,应该可以解决问题。如果图像的比例宽度超过屏幕宽度,您可能会遇到一些间距问题,但它应该适合您。Set the width of the ImageButtons to
fill_parent
and use scaletypefitStart
for the images that hug the left margin, andfitEnd
for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.