Android 在LinearLayout中将文本视图置于图像视图之上

Android 在LinearLayout中将文本视图置于图像视图之上,android,css,image,android-layout,Android,Css,Image,Android Layout,我一直在学习一些xml代码,希望您能帮助我 这就是我现在需要/已经做的 我有3张650px宽的图片,我需要它们一张接一张,同时保持纵横比 我用下面的代码做到了这一点,它看起来应该是这样的。没问题 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" and

我一直在学习一些xml代码,希望您能帮助我

这就是我现在需要/已经做的

我有3张650px宽的图片,我需要它们一张接一张,同时保持纵横比

我用下面的代码做到了这一点,它看起来应该是这样的。没问题

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="vertical"
android:background="#3afa3f"
android:weightSum="3">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/krk_history"
    android:layout_weight="1"
    android:background="@drawable/povjestkrka_vintage_yellow600" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/krk_frankopan"
    android:background="@drawable/frankopan2_vintage_sundown600" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/krk_turizam"
    android:background="@drawable/krk_turizam_vintage"/>

问题是。我需要在每个图像的左下角放置三个文本视图。想象一下,它就像一个图像菜单,每个图像上都有文字描述

所以,我不能使用相对或帧布局,因为图像之间不能很好地配合:)在纵横比等之间总是有差距。我需要权重和

其次,我不能在photoshop中硬编码每个图像上的文本,因为应用程序将被翻译,文本需要针对每种语言进行更改

对于每种语言,我可以有不同的图像,但我尽量避免这种情况

啊,是的,还有最后一件事。 我试图将整个线性布局放在相对布局中,并将文本放在两者之间,但文本显示在图像下方,我无法将其放在前面

有什么建议吗

非常感谢,
Xsonz

线性布局中取三个
FrameLayout
,在
FrameLayout中保留图像视图和文本视图


或者,您可以使用
RelativeLayout
并为视图提供依赖关系

您可以在线性布局中使用框架布局。在线性布局内创建imageview,并在其下方的另一个框架布局中创建textview

<FrameLayout
    android:id="@+id/frameLayout_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/krk_history"
        android:layout_weight="1"
        android:background="@drawable/povjestkrka_vintage_yellow600" />
</FrameLayout>
<FrameLayout
    android:id="@+id/frameLayout_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="-5dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="string" />
</FrameLayout>

只需复制以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:padding="5dp"
        android:text="Text 1" />

</FrameLayout>
 <FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:padding="5dp"
        android:text="Text 2" />

</FrameLayout>
 <FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:padding="5dp"
        android:text="Text 3" />

</FrameLayout>


像这样的东西可能适合你

//your layout -> LinearLayout>
....
<RelativeLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image1"
        android:layout_weight="1"
        android:background="@drawable/A1" />
    <TextView android:id="@+id/textview1"
        android:layout_margin="0dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Text"
        android:layout_below="@id/image1"
        android:layout_alignLeft="@id/image1" />

</RelativeLayout>
// end your LinearLayout
//您的布局->线性布局>
....
//结束你的线性布局
您可以使用来处理这些参数。相对布局允许您控制元素之间的“相对”定位。 你可以把你的图像并排放出来,或者以任何你想要的方式


然后您可以动态调整文本。

首先使用ImageView和TextView构建一个RelativeLayout/FrameLayout,然后在线性布局中包含这样的布局(而不是ImageView)
或imagewithtext.xml:
然后
是什么阻止了您直接在应用程序中以编程方式绘制图像中的文本?使用
Canvas
Paint
katrio,谢谢你的代码。我对它进行了一些调整以满足我的需要,但这个概念是正确的。您的回答与“Selvin”的评论相同,不确定谁先回答,但我会选择您的作为解决方案。