【问题标题】:android ImageButton scale and maintain aspect ratioandroid ImageButton 缩放并保持纵横比
【发布时间】:2011-08-24 23:36:28
【问题描述】:

我正在尝试布局一个屏幕...

基本上,我试图在屏幕上垂直均匀分布 4 个 ImageButton 对象...我使用此 here 来均匀分布项目,但现在让图像缩放但保持纵横比很糟糕...如果我使用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>

希望有人可以提供帮助。

【问题讨论】:

    标签: android layout scale imagebutton aspect-ratio


    【解决方案1】:

    将 ImageButtons 的宽度设置为 fill_parent 并使用 scaletype fitStart 用于紧靠左边距的图像,fitEnd 用于右侧的图像。至少就您的示例图像而言,应该做到这一点。如果图像的比例宽度超过屏幕宽度,您可能会遇到一些间距问题,但它应该适合您。

    【讨论】:

    • 非常感谢您的快速响应...我试过了,它不会放大图像...而且,如果屏幕太小,它不会缩小它。
    • 不过,我只是在所见即所得编辑器中检查过它……也许我不应该相信这一点。
    • 是的,不要相信它,根据我的经验,这种事情很糟糕。在模拟器或设备上试用。
    • 哦,是的...这太疯狂了...模拟器显示一切都很好...非常感谢您的帮助!
    • 到今天仍然如此吗?
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多