android - 使用 FrameLayout 将 TextView 置于 ImageView 之上

标签 android android-layout android-relativelayout android-framelayout

下面是我设计 xml 的方式。现在我正在尝试在下面显示的白框中放置一个 TextView 。但是受到 FrameLayout 的限制(至少我是这样认为的),我需要对值进行硬编码以使 TextView 适合中间或白框内的某些位置。我不能为此目的使用 Relative 或其他布局,因为我通过试验了解到这整个是一个图像。

这是我的布局,

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:visibility="visible" android:layout_marginTop="60dip"
    android:layout_gravity="center" android:id="@+id/xxx">

    <ImageView android:id="@+id/calloutquizImage" 
        android:background="@drawable/callout" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:scaleType="fitCenter" />

    <ImageView android:id="@+id/triviaImage"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignTop="@+id/calloutquizImage" android:layout_gravity="left"
        android:src="@drawable/trivia" android:background="@drawable/trivia"
        android:layout_marginTop="50dip" android:layout_marginLeft="85dip"></ImageView>

    <TextView android:text="TextView" android:id="@+id/triviAnswerText"
        android:layout_marginTop="125dip" android:layout_marginLeft="85dip"
        android:layout_gravity="left" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textColor="#000000"
        android:typeface="sans"></TextView>

    <ImageButton android:id="@+id/triviaanswercloseButton"
        android:src="@drawable/closebtn" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/closebtn"
        android:layout_marginRight="8dip" android:layout_marginTop="43dip"
        android:layout_gravity="right" android:onClick="triviaanswerClose"></ImageButton>
    <ImageView android:id="@+id/buttontoclose"
        android:layout_gravity="left"
        android:visibility="visible" android:onClick="triviaanswerClose" 
        android:layout_marginTop="50dip" android:layout_marginLeft="75dip"
        android:layout_width="230dip" android:layout_height="170dip"></ImageView>
</FrameLayout>

正因为如此, TextView 在不同的手机中看起来在不同的位置。

有没有想过可以为此做些什么?

下面是我的图片:

enter image description here

最佳答案

我认为你没有做正确的事。如果您希望文本显示在白框内(或者甚至调整它的大小,如果有太多文本适合它的话)- 您仍然可以避免任何布局广告使用只有一个 TextView 来实现。

请看一下Android中的NinePatch图像是什么: http://developer.android.com/reference/android/graphics/NinePatch.html

http://developer.android.com/tools/help/draw9patch.html - 绘图工具

所以基本上你只需要 1 个 textView 和你的图像,正确转换为带有第二个链接的 9-patch。 (基本上 - 只需在图像边框上添加几个黑色像素)。 不要只是将这个 9-patch 设置为 textView 的背景。它会将文本放在您需要的位置,如果您在 9-patch 中定义的话,它会缩小白框。

更新: 请找到生成的屏幕截图: enter image description here

如你所见,textView不处理 WhiteBox"本身,用文本填充它并在必要时调整框的大小。

这是让它工作的方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
        android:id="@+id/first"
        android:background="@drawable/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Manymanymany text 
        Manymanymany text 
        Manymanymany text 
        Manymanymany text 
        Manymanymany text 
        Manymanymany text" />

    <TextView
        android:layout_below="@+id/first"
        android:background="@drawable/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not so many text" />
</RelativeLayout>

这是您的图像,已转换为 9patch。只需将它放在“drawable/”文件夹中。注意:它必须有“back.9.png”名称。 enter image description here

有关 9patch 工作原理的详细信息,您可以查看上面的链接。主要思想:通过在左边框和上边框上制作黑点 - 您可以指定在必须放大图像时拉伸(stretch)图像的哪一部分。通过在右侧/底部制作点,您可以告诉 View 放置内容的位置。在我们的例子中,内容是 TextView 的文本。

希望对你有帮助,祝你好运

关于android - 使用 FrameLayout 将 TextView 置于 ImageView 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12245644/

相关文章:

Android:如何在自定义 tabLayout 中滚动填充标签?

android - Firebase 作业调度程序是否需要互联网访问来安排作业?

java - recyclerview android 中的 2-vertical 和 2-horizo​​ntal 卡片 View

安卓布局。使用 <include> 使表格布局的列大小相等

java - Gradle 和 Realm 库的编译问题

java - 使用 Jsoup 解析 html 并删除具有特定样式的跨度

android - 可绘制 2 种纯色

android - relativeLayout问题-图像与ScrollView重叠

android - RelativeLayout 在 Android 中使用 ViewPager 不可见

Android recyclerView 项目布局参数不起作用