2013-03-05 249 views
3

我是Android新手,一切都很顺利,直到现在我不得不处理布局。 我想是这样的布局:ImageView:适当缩放按钮

布局应该如何: 图像应适当比例,并保持其比例。

这就是为什么我尝试这个XML代码,但它不按我想要的方式工作。图像保持其比率,但在左侧和右侧超出垂直当屏幕中,并在顶部和底部切割时的水平:

布局如何,它是: enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:background="@color/background"> 


    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_gravity="center" 
     android:src="@drawable/logo" 
     android:gravity="center" 
     android:scaleType="centerCrop" 
     android:contentDescription="@string/app_logo" 
     android:layout_weight="2.5" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="4" 
     android:gravity="center"> 

     <Button 
      android:id="@+id/id1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginBottom="20dp" 
      android:layout_marginLeft="50dp" 
      android:layout_marginRight="50dp" 
      android:text="@string/text1" 
      android:textSize="40sp" 
     /> 

      <Button 
      android:id="@+id/id2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="50dp" 
      android:layout_marginRight="50dp" 
      android:text="@string/text2" 
      /> 

     <Button 
      android:id="@+id/id3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="50dp" 
      android:layout_marginRight="50dp" 
      android:layout_gravity="center" 
      android:text="@string/text3" /> 
    </LinearLayout> 
</LinearLayout> 

我GOOGLE了很多,但只是找不到我在找什么。 请帮忙!

+1

你能提供的是什么情况,现在的近似图片,应该怎么样子呢? – g00dy 2013-03-05 08:34:37

+0

请分享您的预期输出图片,,以便我们能够弄清楚 – 2013-03-05 08:41:37

+0

我为该问题添加了一张图片。 – JustABit 2013-03-05 08:44:28

回答

1

看起来android:scaleType="fitCenter"是你正在寻找

+0

这工作得非常好!非常感谢你! – JustABit 2013-03-05 09:34:02

0

你要设计2布局你垂直状态和水平状态,并把它们放在两个不同的文件夹中的Android项目:

,而你把你的常规布局的布局文件夹,你应该把景观布局layout-land文件夹。

+0

但是这个布局并不适合他们。 – JustABit 2013-03-05 08:55:22

0

尝试将android:scaleType="fitXY"设置为您的ImageView

+0

但它并没有保持它的比例。 – JustABit 2013-03-05 08:51:29