[英]How to arrange three views horizontally?
我有三個 3 textview
水平。 第一個和第三個textview
是靜態的,意味着它的特定width
但第二個是動態的。 當第二個textview
獲取更多數據時,然后隱藏其他views
。 為了解決這個問題,我使用了LinearLayout
。首先給所有視圖賦予權重,但這會在view
之間產生間距問題。 然后我嘗試為個人視圖增加weight
,然后最后一個視圖沒有顯示。 LinearLayout
不是強制性的。 我想之間的間距相等views
和每個視圖應該是可見的,就像這樣。
這是我的代碼
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="1"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
android:layout_marginLeft="@dimen/_5sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ronald Flores long text xxxxxxxxxxxxxxx"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
android:maxLength="120"
android:ellipsize="end"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="4"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
android:layout_marginLeft="@dimen/_5sdp"
/>
</LinearLayout>
輸出:
提前致謝 。 我會很感激你的答案。
您是否必須使用 LinearLayout ? 如果沒有,您可以嘗試使用 TableLayout 和權重系統,結果在我看來相當不錯。
這是使用 TableLayout 的 XML 代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center">
<TableRow
android:id="@+id/table_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:background="@android:color/black"
android:text="tv1"
android:layout_weight="2"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@android:color/holo_green_dark"
android:text="tv2"
android:textAlignment="center"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="@android:color/black"
android:layout_weight="2"
android:text="tv3"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
然后,當您操作數據並需要拉伸第二個文本視圖時,您可以以編程方式進行,例如:
TableRow.LayoutParams layoutParams;
layoutParams = (TableRow.LayoutParams)tv2.getLayoutParams();
layoutParams.weight = 4;
tv2.setLayoutParams(layoutParams);
以下是第二個 textview 的權重為 2 之前和 4 之后的差異:
如果您不希望在第二個拉伸時縮小第一個和第三個 textview,您可以嘗試使用每個視圖的 layout_span 屬性
我希望這會有所幫助,讓我們知道它是否有效。
----更新 1 ----
顯然,在你的第二個 textview 上使用一個與 maxLength 限制相結合的 RelativeLayout 可以實現它,你可以試試這個:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="@android:color/black"
android:maxLines="1"
android:text="tv1"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_toEndOf="@id/tv1"
android:background="@android:color/holo_green_dark"
android:ellipsize="end"
android:maxLength="@dimen/max_char_40"
android:maxLines="1"
android:text="Ronald Flores dfdfdfdfdbnbnbnnbnbnbffdddfdfddffdf"
android:textAlignment="center"
android:textColor="@android:color/holo_red_light"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_toEndOf="@id/tv2"
android:background="@android:color/black"
android:maxLines="1"
android:text="tv3"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
嘗試這個 :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:text="1"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
android:layout_marginLeft="@dimen/_5sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ronald Flores"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="4"
android:textColor="#9A9A9A"
android:textSize="@dimen/text14sp"
android:fontFamily="@font/montserrat"
android:maxLines="1"
android:layout_marginLeft="@dimen/_5sdp"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:gravity="center"
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView " />
<TextView
android:gravity="center"
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.