简体   繁体   English

在ImageView的顶部设置TextView-Android布局

[英]Setting TextView on top of ImageView - Android Layouts

I'm building an android app and I am trying to set my layout without any success. 我正在构建一个android应用程序,并且试图设置布局没有成功。 I searched everywhere and I can't seem to find any solution. 我到处搜索,但似乎找不到任何解决方案。

I need to place 2 text views on top of an image. 我需要在图像上方放置2个文本视图。 the first text view need to be about 10dp above the vertical center of the image. 第一个文本视图需要在图片的垂直中心上方大约10dp的位置。 and the second text view should be below text view 1. 第二个文本视图应在文本视图1下方。

here's my code so far, though it doesn't work. 到目前为止,这是我的代码,尽管它不起作用。 can anyone here tell me what I'm missing or what am I doing wrong? 有人可以告诉我我想念什么或我做错了什么吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.boromedia.parve.MainActivity" >

<RelativeLayout
    android:id="@+id/blueOvalLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="75sp" >

    <ImageView
        android:id="@+id/blueOval"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:src="@drawable/blueoval" />

    <ImageView
        android:id="@+id/greenOval"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:src="@drawable/greenoval_small"
        android:visibility="gone" />

    <TextView
        android:id="@+id/greenOvalText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="65dp"
        android:gravity="center|center_vertical"
        android:text="@string/counter_activity_oval_done"
        android:textColor="#fff"
        android:textSize="28sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/blueOvalText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/counter_activity_oval_text1"
        android:textColor="#fff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/blueOvalTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/blueOvalText"
        android:layout_centerHorizontal="true"
        android:text="@string/counter_default"
        android:textColor="#fff"
        android:textSize="25sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

<TextView
    android:id="@+id/head_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="15dp"
    android:gravity="center_vertical|center"
    android:text="@string/main_title"
    android:textSize="30sp" />

<Button
    android:id="@+id/stopButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/transparent"
    android:text="@string/counter_activity_stop_button"
    android:textSize="20sp" />

现在的样子

Try this workaround: Change your textView for: 尝试以下解决方法:将textView更改为:

   <RelativeLayout
        android:id="@+id/wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp" >

        <TextView
            android:id="@+id/blueOvalText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/counter_activity_oval_text1"
            android:textColor="#fff"
            android:textSize="18sp" />
    </RelativeLayout>

This is a wrapper for setting the margin of 10dp. 这是用于设置10dp页边距的包装器。 Don't forget that your second textview should be below the wrapper, not below the textview! 不要忘记您的第二个textview应该在包装器下方,而不是在textview下方!

I hope this would help;) 我希望这会有所帮助;)

You can put your 2 TextView in a LinearLayout which will be centered in the parent : 您可以将2个TextView放在LinearLayout中,该布局将位于父级的中心:

<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:layout_centerInParent="true" >

<TextView
    android:id="@+id/blueOvalText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/counter_activity_oval_text1"
    android:textColor="#fff"
    android:textSize="18sp" />

<TextView
    android:id="@+id/blueOvalTimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/blueOvalText"
    android:layout_centerHorizontal="true"
    android:text="@string/counter_default"
    android:textColor="#fff"
    android:textSize="25sp" />
</LinearLayout>

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

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