[英]Android - Center Textview in ImageView of Relative Layout
我有一個帶有ImageView的相對布局,並且想要在活動布局xml文件中將TextView放在ImageView的中心。
這是圖像視圖,然后是我為TextView嘗試的圖像
這是相對布局的內部:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="170dp"
android:minWidth="170dp"
android:id="@+id/logoBackground"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="33dp"
android:contentDescription="@string/content_description_useless"
android:background="#ffffd0cd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capital_u"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_alignTop="@+id/logoBackground"
android:layout_alignLeft="@+id/logoBackground"
android:layout_alignStart="@+id/logoBackground"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="65dp"/>
我認為看起來不錯,但marginTop為65難道不應該是imageView高度的一半嗎?
我在這里先向您的幫助表示感謝。
它不起作用,因為您告訴TextView位於父級RelativeLayout的中心,但是ImageView附加在父級的左上角。
一種方法是使用RelativeLayout的alignXyz屬性將TextView強制為ImageView的確切大小。 然后,將gravity屬性設置為center,這將使文本出現在TextView邊界的中心。 只要在所有維度上ImageView都大於TextView,這應該起作用。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capital_u"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_alignTop="@+id/logoBackground"
android:layout_alignLeft="@+id/logoBackground"
android:layout_alignRight="@+id/logoBackground"
android:layout_alignBottom="@+id/logoBackground"
android:gravity="center"/>
那對你有用嗎?
我看到的一個問題是這一行:
android:layout_centerInParent="@+id/logoBackground"
layout_centerInParent的值應為true或false。 我猜測它的評估結果為假,位於左上角。 如果將其設置為true,它將自身居中在父級的中間(相對布局)。 要將其在ImageView中居中,您需要使相對布局的大小與ImageView相同。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="170dp"
android:minWidth="170dp"
android:id="@+id/logoBackground"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="33dp"
android:contentDescription="@string/content_description_useless"
android:background="#ffffd0cd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capital_u"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
試試這個到您的textview: android:layout_centerInParent="true"
這樣就可以:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="170dp"
android:minWidth="170dp"
android:id="@+id/logoBackground"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="33dp"
android:contentDescription="@string/content_description_useless"
android:background="#ffffd0cd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capital_u"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_alignTop="@+id/logoBackground"
android:layout_alignLeft="@+id/logoBackground"
android:layout_alignStart="@+id/logoBackground"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="65dp"
android:layout_centerInParent="true"/>
試試這個代碼:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:scaleType="centerCrop"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:id="@+id/your_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="1" />
</RelativeLayout>
為此,您必須使用“ Frame Layout
。
供參考,請參見此處www.developer.android.com/reference/android/widget/FrameLayout.html
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Frame Text"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.