繁体   English   中英

在LinearLayout中将Imageview放在Imageview上

[英]Put a textview over Imageview in LinearLayout

我坚持使用一些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"/>

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

所以,我不能使用相对或框架布局,因为图像不能很好地相互配合:)方面之间总是存在差距。我需要权重。

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

我可以为每种语言提供不同的图像,但我试图避免这种情况。

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

有什么建议么?

非常感谢,Xsonz

只需复制一下:

<?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>

LinearLayout取三个FrameLayout ,在FrameLayout内部保留ImageView和TextView。

或者其他您可以使用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>

这样的事可能适合你。

//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

您可以使用相对布局参数android.com来播放这些参数。 相对布局允许您控制元素之间的“相对”定位。 您可以将图像并排放置,也可以以任何方式放置。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM