2013-07-14 135 views
1

在Android活动中,我想放置一个徽标以水平填充屏幕;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_widthfill_parent是足够的,但很明显不。我还要做什么?

回答

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> 
0

使用match_parent而非fill_parent做chnages,因为它deprected。也使用在元素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