[英]Android LinearLayout Align Right
我有氣泡,氣泡的重心是中心。 如果我寫一條消息,適配器將重力設置為左或右,但氣泡始終向左。
bubble.setGravity(chatMessageObj.left ? Gravity.LEFT : Gravity.RIGHT);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/speechbubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/bubble" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
layout_gravity
!= gravity
布局重力是相對於父級的; 重力是關於內容的。 因此layout_gravity
中心將始終將視圖定位在其父對象的中心內; 而gravity
LEFT
或RIGHT
會將內容在該視圖中向左或向右定位。
您的代碼的一個問題是父級LinearLayout
沒有方向!
此布局的其余部分是什么樣的? 還是這個linearlayout bubble
是其中唯一的其他視圖?
嘗試使用LayoutParams:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = chatMessageObj.left ? Gravity.LEFT : Gravity.RIGHT;
bubble.setLayoutParams(params);
我將第二個LinearLayout的layout_width設置為wrap_content,將第一個LinearLayout的重力(不是layout_gravity)設置為右。 現在工作了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.