Domanda

I have created an analog clock and now I want to add text inside it. This is my main.xml. I can add text to the textview but that appears above or below the clock and not inside it. Is there a way to add text inside the clock? I checked AnalogClock class on android and I do not see any such option

 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/analog_label"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <AnalogClock
        android:id="@+id/analogClock"
       android:text="@string/analog_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
È stato utile?

Soluzione

The Niko's answer is completely correct, but in case you don't want to use FrameLayout, all you have to do is using android:layout_centerInParent="true", check the below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     >

    <AnalogClock
        android:id="@+id/analogClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:text="YouText"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/black" />

</RelativeLayout>

And this is the result:

enter image description here

Altri suggerimenti

Wrap both inside FrameLayout or u can use RelativeLayout also:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <AnalogClock
        android:id="@+id/analogClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/analog_label" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="@string/analog_label"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/black" />
</FrameLayout>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top