如何适应布局背景图像

时间:2012-08-13 10:19:12

标签: android android-layout

我想用一个图像(1280x800)用于两个不同屏幕尺寸和相同密度的设备。

屏幕尺寸为1280x800320x480

图像是九个补丁(带内容区域)。我想将图像用于行的背景,它具有textview。并且不要对文本使用手动填充(我想使用内容区域)。

如果我使用这个xml:

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pak.name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@layout/menu_selector"
android:gravity="right|center_vertical">

<pack.name.TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:text="blah blah"/>

模拟器中的

输出是:

http://ivm.neru9.com/pic/2ccf95a7f2e1.png

如果我使用这个xml:

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pack.name."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="15dp"
android:paddingLeft="15dp">

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@layout/menu_selector"
    android:scaleType="fitCenter"
    />

<pack.name..TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:gravity="right|center_vertical"
    android:text="blah blah blah"/>

输出是:

http://ivm.neru9.com/pic/f20f7f5d2e7a.png

这是listview xml(我觉得这个问题不重要):

<ListView
    android:id = "@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="@null"
    android:dividerHeight="3dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent"
    />

及其在布局文件夹中的menu_selector xml代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" />
<!-- selected -->
<item android:state_selected="true" android:drawable="@drawable/bg_menu_focused" />
<!-- selected & pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" />
<!-- default -->
<item android:drawable="@drawable/bg_menu" /> 

2 个答案:

答案 0 :(得分:2)

check out this project

基本上问题是你的9补丁图片,我只是修改你的图像以获得你的问题的欲望表现。

我建议你使用bg_menu277x77进行1280x800和bg_menu144x40进行上述链接项目的320x480分辨率。只是为了测试,您可以将两个图像交换为分辨率并尝试。

贝娄我尽力解释9补丁图片,其他精彩教程&amp;你可以谷歌的信息:)

enter image description here

答案 1 :(得分:0)

如果我正确理解您的要求,那么您需要一个列表视图,列表视图的每一行都包含一个背景和一个文本视图来显示一些文本。
您不必为此目的使用图像视图。 您需要做的就是为布局容器和文本视图放置背景。

以下是如何实现这一点:

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pack.name."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:background="@drawable/some_drawable or any selector">

<pack.name..TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:gravity="right|center_vertical"
    android:text="blah blah blah"/>
</RelativeLayout>

您的代码存在的问题是您使用fill_parent作为各个行项的高度和宽度。相反,你应该使用fill_parent作为width和wrap_content或一些dp值作为高度,如上面的代码所示。

我希望这会有所帮助:)

相关问题