![](/img/trans.png)
[英]How can i display programmatically a textview next to a button in a RelativeLayout in Android?
[英]Android RelativeLayout Button above TextView
有2個文本視圖,1個緊挨着另一個(使用layout_toLeftOf屬性),我試圖在每個文本視圖上方放置一個按鈕:1)如果在上面使用,請在按鈕上使用alignLeft和alignRight屬性,按鈕的寬度和高度會放大以適合文本視圖的大小2)因此,我想到了使用線性布局,使其適合textview的大小,然后放置一個帶有layout_gravity =“ center”的按鈕,但是它沒有居中,而是對齊的向左轉。
這是代碼(所有代碼都在RelativeLayout中):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/countdown_hours_tv"
android:layout_alignLeft="@id/countdown_hours_tv"
android:layout_alignRight="@id/countdown_hours_tv">
<Button
android:layout_width="@dimen/plus_minus_button_size"
android:layout_height="@dimen/plus_minus_button_size"
android:layout_gravity="center"
android:background="@drawable/plus_sign_button"
android:onClick="increaseHourClicked"/>
</LinearLayout>
<Button
android:layout_width="@dimen/plus_minus_button_size"
android:layout_height="@dimen/plus_minus_button_size"
android:layout_below="@id/countdown_hours_tv"
android:layout_alignLeft="@id/countdown_hours_tv"
android:layout_alignRight="@id/countdown_hours_tv"
android:background="@drawable/minus_sign_button"
android:onClick="decreaseHourClicked"/>
嘗試使用FrameLayout將一個視圖放在另一個視圖的上方,在您的情況下,這是在textView頂部的一個按鈕。
嘗試在具有垂直方向的LinearLayout中添加Button和TextView。 像這樣:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/layout1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.