Android图像宽度填充布局

时间:2013-07-14 08:03:08

标签: android-layout

在Android活动中,我想放置一个徽标以横向填充屏幕;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:background="#FF0000"
              android:id="@+id/layout"
>  
    <ImageButton
        android:background="#124644" 
        android:src="@+drawable/androidlogo" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"             
        android:id="@+id/LogoImage" 
    >
    </ImageButton>  
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:textColor="#FFFFFF"
        android:background="#000000"
        android:textSize="34sp" 
        android:gravity="center"
        android:typeface="sans"
        android:id="@+id/TitleText"
        android:text="Some Test"
        android:textStyle="bold" 
    >
    </TextView> 
</LinearLayout> 

在一个小屏幕上填充:

Emulator with MDPI screen

但不是在大屏幕上:

Emulator with MDPI screen

我希望将layout_width设置为fill_parent就足够了,但显然不是。我还能做什么?

2 个答案:

答案 0 :(得分:0)

尝试使用重量和比例类型。

这是修改过的代码......根据您的需要进行编辑

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:background="#FF0000"
              android:id="@+id/layout"
>  
    <ImageButton
        android:background="#124644" 
        android:src="@drawable/ic_launcher" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"             
        android:id="@+id/LogoImage"
        android:layout_weight=".8" 
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
    >
    </ImageButton>  
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:textColor="#FFFFFF"
        android:background="#000000"
        android:textSize="34sp" 
        android:gravity="center"
        android:typeface="sans"
        android:id="@+id/TitleText"
        android:text="Some Test"
        android:textStyle="bold" 
    >
    </TextView> 
</LinearLayout> 

答案 1 :(得分:0)

使用match_parent而不是fill_parent,因为它已被弃用。也可以在元素ImageButton中使用该元素的属性layout_weight适合整个屏幕,TextView可见:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/LogoImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="#124644"
        android:scaleType="fitXY"
        android:src="@drawable/androidlogo" >
    </ImageButton>

    <TextView
        android:id="@+id/TitleText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:gravity="center"
        android:text="Some Test"
        android:textColor="#FFFFFF"
        android:textSize="34sp"
        android:textStyle="bold"
        android:typeface="sans" >
    </TextView>

</LinearLayout>

enter image description here