Android 如何适应布局背景图像
我想使用一个图像(1280x800)为两个不同屏幕大小和相同密度的设备 屏幕大小为Android 如何适应布局背景图像,android,android-layout,Android,Android Layout,我想使用一个图像(1280x800)为两个不同屏幕大小和相同密度的设备 屏幕大小为1280x800和320x480 图像为九个补丁(带内容区)。我想使用图像作为具有文本视图的行的背景。不要对文本使用手动填充(我想使用内容区域) 如果使用此xml: <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:foo="http://schemas.android.com/apk/res/
1280x800
和320x480
图像为九个补丁(带内容区)。我想使用图像作为具有文本视图的行的背景。不要对文本使用手动填充(我想使用内容区域)
如果使用此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"/>
emulator中的输出为:
如果我使用这个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"/>
输出为:
这是listview xml(我认为这对于这个问题并不重要):
以及布局文件夹中的菜单选择器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" />
如果我正确理解了您的需求,那么您需要一个列表视图,列表视图的每一行都包含一个背景和一个文本视图来显示一些文本。您不必为此目的使用图像视图。 您只需为布局容器和文本视图放置背景。 以下是实现此目标的方法:
<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作为宽度,使用wrap_内容或一些dp值作为高度,如上述代码所示我希望这会有帮助:)请 基本上,问题是你的9补丁图像,我只是修改你的图像的欲望性能为您的问题 我建议您使用bg_menu277x77用于1280x800,使用bg_menu144x40用于上述链接项目的320x480分辨率。为了进行测试,您可以将两幅图像交换为两种分辨率,然后再试一次 贝娄我尽我所能解释9补丁图片,其他伟大的教程和信息,你可以谷歌:)
tnx,请注意。关于第1点:由于图像底部有阴影,我必须拉伸图像毕竟,这并不能解决我的问题。我说要使用一个有9个补丁且可伸缩的大图像。我认为这是不可能的(两种技术:布局背景中的A-9补丁。B-可伸缩图像并对某些屏幕大小使用一个图像)。
两种技术:布局背景中的A-9补丁。B-可缩放图像,并对某些屏幕尺寸使用一个图像
,是的,同意克服此问题,您必须分别为较小和较大的屏幕创建9patch图像。如果你想要一张适合所有人的图片,你可以保留一个像素+1:)@rajpara i检查您所附的项目。但问题是文本不在内容区。@CooLi3oY然后将textview的布局保持为重力center@rajpara不,这不是个好办法。因为我想在内容区的右边设置文本。我想要这个:| | | | | | | | | | | | | | | | | | | | | | | | |内容| A | r | e | | | | A |它在银河模拟器上的提示:注意填充。行图像的左侧和右侧没有填充,但我们设置了它!您使用的是启用默认多行属性的文本视图,这就是为什么文本过长时行图像会放大的原因。为了解决这个问题,您应该使用文本视图的android:singleLine=“true”功能。关于填充问题:我尝试创建一个带有填充的9补丁图像,但是图像无法正常工作。因此,您应该在xml定义中使用android:gravity=“center”或padding。如果我使用singleLine=true属性,则输出:屏幕大小是固定的,因此我们不能在同一行上显示很长的文本,相反,您应该设置最大宽度和单行true。当用户单击列表项时,可以显示详细信息。我使用的是一个九块补丁图像作为行的背景。与我在这篇文章上的最后一条评论相关,如果不使用singleLine=true,则输出:
<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>