Android 覆盖TextView和ImageView的宽度相等
我有一个文本视图(文本)覆盖我的图像视图(图像)。我的问题是它们的宽度不同。换句话说,我希望红色区域显示为白色: 以下是我目前的布局文件:Android 覆盖TextView和ImageView的宽度相等,android,android-layout,android-xml,Android,Android Layout,Android Xml,我有一个文本视图(文本)覆盖我的图像视图(图像)。我的问题是它们的宽度不同。换句话说,我希望红色区域显示为白色: 以下是我目前的布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="10dp"
android:textSize="25sp"
android:gravity="center"
android:background="#9988FF88"
android:textColor="#000000"/>
</RelativeLayout>
我也在寻找程序解决方案,但也无法解决:
image = (ImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
text.setWidth(image.getWidth());
有没有办法去掉我的红色区域?每一个想法都会受到赞赏,我会很高兴看到xml解决方案或编程解决方案!提前感谢:)
编辑
我应该指定ImageView中的图像确实被替换了很多,这样一些图像的宽度就填满了整个水平屏幕。因此,在某些图像上,左侧和右侧应有边距,但在其他图像上则不应有边距(换句话说,标题应始终与图像一样大)。(顺便说一句:谢谢你的快速回复!)
更新
我解决了这个问题,只是给了标题一个白色的背景色。您的imageview设置为与父项匹配,但imageview周围仍然有白色区域。你确定你的图像周围没有白色区域吗 另外,尝试使用填充父项而不是匹配父项。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
<!--android:padding="10dp" -->
android:textSize="25sp"
android:gravity="center"
android:background="#9988FF88"
android:textColor="#000000"/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
android:textSize=“25sp”
android:gravity=“center”
android:background=“#9988FF88”
android:textColor=“#000000”/
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="26dp"
android:background="#FFFFFF" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="10dp"
android:textSize="25sp"
android:gravity="center"
android:background="#9988FF88"
android:textColor="#000000"/>
</RelativeLayout>
我能想到的最佳解决方案是:
- 从图像中剪下白色边框(然后在yout ImageView中设置一些边距,以模拟边框)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="10dp"
android:textSize="25sp"
android:gravity="center"
android:background="#9988FF88"
android:textColor="#000000"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
解决方案可能是:复制图像的顶部,切割天空,然后用半透明的绿松石色填充孔洞。然后将第二幅图像(具有半透明颜色和白色边框)设置为TextView的背景。好的,我理解你的意思,但图像可能会发生很大变化,因此我必须经常剪切图像,实际上我非常喜欢我的透明标题;)但是谢谢你的主意!我添加了一个更好的解决方案作为答案。关键概念:从图像中删除边框,然后使用ImageView和TextView中的边距模拟边框,以匹配完美对齐。fill\u parent
与match\u parent
相同,但从API级别8开始就被弃用。您好,白色边框不是图像的一部分,这是我相对布局的背景色。边距可以用于静态图像,但由于我的边距可能会发生变化,并且高度和宽度可能会发生变化,因此这对我不适用。不过,谢谢+1这是可行的,但它会将我的图像强制为固定大小。我更喜欢可变大小的图像,因为它经常变化,但谢谢!Pat,如果您从ImageView中删除scaleType,那么这将适用于不同大小的图像,但它没有TextView的确切宽度。另一种方法是删除scaleType,将图像放在“android:src”属性中,并在“android:background”属性中放置一些颜色或公共图像。这将使您的imageview与TextView具有相同的宽度,并可以显示不同大小的图像如果我这样做,我会在图像的每个边框上留一个边距,这样它的大小就会变小,并且不适合显示在屏幕顶部。。。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="10dp"
android:textSize="25sp"
android:gravity="center"
android:background="#9988FF88"
android:textColor="#000000"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>