簡體   English   中英

如何在一個LinearLayout內的多個圖像上設置文本?

[英]How to set text over multiple images which is inside one LinearLayout?

我想在圖像上放一些文字。 有人可以幫我嗎?

我有六個矩形框,我想在每個框上放一些文本。 我已經使用ImageView制作了矩形。

請找到以下mainactivity.xml代碼:

  <?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#ffffff"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.vimal.edkul.StudentProfile">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout1">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/title">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:src="@drawable/pic13" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="User Profile"
                android:textSize="20sp"
                android:textColor="@android:color/white" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/ic_action_overflow" />
        </android.support.v7.widget.Toolbar>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout2"
        android:layout_gravity="center"
        android:layout_below="@+id/layout1"
        android:padding="30dp">
        <FrameLayout
            android:layout_height="100dp"
            android:layout_width="130dp">
            <View
                android:background="@drawable/rectangle"
                android:clickable="false"
                android:id="@+id/view1"
                android:layout_height="match_parent"
                android:layout_width="match_parent" />
            <TextView
                android:gravity="center"
                android:id="@+id/text1"
                android:textColor="#000"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:text="Some text" />
        </FrameLayout>
        <FrameLayout
            android:layout_height="100dp"
            android:layout_width="130dp">
            <View
                android:background="@drawable/rectangle"
                android:clickable="false"
                android:id="@+id/view2"
                android:layout_height="match_parent"
                android:layout_width="match_parent" />
            <TextView
                android:gravity="center"
                android:id="@+id/text2"
                android:textColor="#000"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:text="Some text" />
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>

rectangle.xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
    <solid android:color="@android:color/transparent"/>
    <corners android:radius="12px"/>
    <stroke  android:width="2dip" android:color="#000000"/>
</shape>

您可以使用布局的以下部分代替每個ImageView

<FrameLayout
    android:layout_height="150dp"
    android:layout_width="100dp" >

    <View
        android:background="@drawable/rectangle"
        android:clickable="false"
        android:id="@+id/buttonVote"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

    <TextView
        android:gravity="center"
        android:id="@+id/textStatusMessage"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="Some text" />

</FrameLayout>

編輯

您可以在rectangle.xml更改背景。 例如:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/darker_gray" />
    <corners android:radius="12px" />
    <stroke
        android:width="2dip"
        android:color="#000000" />
</shape>

該xml對我來說合適。

您甚至可以刪除FrameLayoutView並為TextView設置背景:

<TextView
        android:id="@+id/text1"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="Some text"
        android:textColor="#000"
        android:background="@drawable/rectangle"/>

PS要顯示簡單圖像,您無需使用ImageView ,只需簡單的View背景即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM