【问题标题】:Placing 2 images side by side with Text overlay将 2 个图像与文本叠加并排放置
【发布时间】:2014-11-14 00:20:49
【问题描述】:

我想将 2 张图片与文字叠加并排放置。 这是 1 张带文字的图片的代码,我想将 2 张图片并排放置,并在底部显示文本叠加层。

代码是

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/overlay_margin"
        android:background="#FFF" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/salogo" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_alignParentLeft="true"
            android:background="#7000"
            android:orientation="vertical"
            android:paddingTop="15dp" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:shadowColor="#000"
                android:shadowDx="3"
                android:shadowDy="3"
                android:shadowRadius="6"
                android:text="Styling Android"
                android:textColor="#FFF"
                android:textSize="36sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="A guide to applying styles and themes to Android apps"
                android:textColor="#CCC"
                android:textSize="12sp" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

我想要这样的视图

【问题讨论】:

  • 什么困扰你,你有什么问题?
  • android:orientation="vertical" 用于放置图像和文本。但是要并排放置 2 个图像,它应该是 android:orientation="horizo​​ntal"。不知道怎么放。
  • 您希望两个块并排并在两个图像上方覆盖不同的文本,例如底部的图片或两个图像并排并在两个图像上覆盖一个文本
  • 是的,2 个不同的图像和 2 个不同的文本。

标签: android xml imageview


【解决方案1】:

您需要从这里使用include Improving Layouts : Reusing

您将需要像这样使用您的布局。如果您想添加更多内容,请将 MainLayout.xml 根包装到另一个 LinearLayoutRelativeLayout

创建您将在Activity 上调用的主布局,将“Hello Android”提取到您的字符串文件中:
MainLayout.xml

<LinearLayout
    android:layout_width=”match_parent”
    android:layout_height=”wrap_content”
    android:orientation="vertical">
     <TextView
         android:layout_width=”match_parent”
         android:layout_height=”wrap_content”
         android:Text="Hello Android"/>
     <LinearLayout
         android:layout_width=”match_parent”
         android:layout_height=”wrap_content”
         android:orientation="horizontal">
         <include layout="@Layout/myTextImageLayout"
                 android:layout_weight="1" />
         <include layout="@Layout/myTextImageLayout" 
                 android:layout_weight="1" />
     </LinearLayout>
</LinearLayout>

然后创建另一个布局,您的大部分内容都将放在其中:
myTextImageLayout.xml(你当前的布局)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/overlay_margin"
    android:background="#FFF" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/salogo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:background="#7000"
        android:orientation="vertical"
        android:paddingTop="15dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:shadowColor="#000"
            android:shadowDx="3"
            android:shadowDy="3"
            android:shadowRadius="6"
            android:text="Styling Android"
            android:textColor="#FFF"
            android:textSize="36sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="A guide to applying styles and themes to Android apps"
            android:textColor="#CCC"
            android:textSize="12sp" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2015-11-16
    • 2021-07-17
    • 2017-10-19
    • 2012-02-26
    相关资源
    最近更新 更多