簡體   English   中英

如何在長布局的線性布局中限制textview的寬度

[英]How to restrict the width of textview in linearlayout for long words

這是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="80dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="50sp"
            android:background="#FFFF00"
            android:ellipsize="end"
            android:maxLines="1"
            tools:text="THIS IS A LONG SENTENCE"
            />

        <RelativeLayout
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:padding="4dp">

        </RelativeLayout>

</LinearLayout>

這是布局結果:

在此處輸入圖片說明

我發現textview將占據鄰居相對布局的空間。

我期望的是:

長句子:

在此處輸入圖片說明

簡而言之:

在此處輸入圖片說明

也就是說,我希望藍色塊的寬度固定並在textview的右邊。 如何做到這一點? 謝謝。

如果您可以將布局的容器從linearlayoutrelativeframelayout ,那么我為您提供了一個解決方案。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="80dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textSize="50sp"
    android:background="#FFFF00"
    android:ellipsize="end"
    android:maxLines="1"
    tools:text="THIS IS A LONG SENTENCE"
    android:layout_marginRight="50dp"
    />

<RelativeLayout
    android:layout_gravity="right"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:background="#0000FF"
    android:padding="4dp">

</RelativeLayout>

根據我的嘗試,它可以根據您的需要進行布局。 解決方案基於framelayout(或relativelayout)的疊加其子級的功能。 由於您希望(藍色塊)relativelayout具有固定寬度,因此我只為相同大小的textview分配了邊距。

希望對您有幫助...

wrap_content設置為LinearLayout寬度,並將權重設置為TextView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/linearLyout"
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="80dp">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:textSize="50sp"
    android:background="#FFFF00"
    android:ellipsize="end"
    android:maxLines="1"
    tools:text="THIS IS A LONG SENTENCE"
    />

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:background="#0000FF"
    android:padding="4dp">

</RelativeLayout>

</LinearLayout>

嘗試改為在Java中動態設置maxWidth。 因為TextView的wrap_content會占用您的RelativeLayout的空間。 因此,為了使您的應用對設備更友好,可以使用DisplayMetrics或簡單地使用getHeight()方法來計算父布局的寬度,然后將maxWidth設置為TextView。 例如:

以下是您提到的帶有附加ID的xml代碼

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/linearLyout"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="80dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="50sp"
        android:background="#FFFF00"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text="THIS IS A LONG SENTENCE"
        />

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:background="#0000FF"
        android:padding="4dp">

    </RelativeLayout>

    </LinearLayout>

在Java中首先找到您的LinearLayout的寬度

    int layoutWidht = linearLayout.getWidth();

計算所需的最大寬度

    int textViewWidthInPixels = layoutWidht -  <some_integer_value_in_pixels_you_wish_for_relativeLayout>

然后將maxWidth設置為java中的textView

    textView.setMaxWidth(textViewWidthInPixels);

希望這可以幫助。 快樂編碼

為您的TextView設置android:layout_weight="1"android:layout_width="0dp"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="80dp">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:textSize="50sp"
            android:background="#FFFF00"
            android:ellipsize="end"
            android:maxLines="1"
            tools:text="THIS IS A LONG SENTENCE"
            />

        <RelativeLayout
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:padding="4dp">

        </RelativeLayout>

</LinearLayout>

這是Android Studio 2.1預覽版上的結果圖像

在此處輸入圖片說明 在此處輸入圖片說明

您也可以像這樣使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <RelativeLayout
        android:id="@+id/layout_test"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="#0000FF"
        android:padding="4dp">

    </RelativeLayout>

    <TextView
        android:id="@+id/text_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/layout_test"
        android:background="#FFFF00"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="50sp"
        tools:text="THIS IS A LONG SENTENCE" />
</RelativeLayout>

它將產生與LinearLayout相同的結果圖像。 希望這個幫助

嘗試這個 !!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp">

<TextView
    android:id="@+id/text_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toLeftOf="@+id/layout_test"
    android:background="#FFFF00"
    android:ellipsize="end"
    android:maxLines="1"
    android:textSize="50sp"
    tools:text="THIS IS A LONG SENTENCE" />

<RelativeLayout
    android:id="@+id/layout_test"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:background="#0000FF"
    android:padding="4dp">

</RelativeLayout>

</RelativeLayout>

它工作完美!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM