Implement the android Linearlayout weight effect via flexbox

拜拜、爱过 提交于 2020-01-16 09:08:12

问题


I want to implement the android layout bellow via flexbox.
android layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400px"
    android:layout_height="200px"
    android:background="#0ee"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"/>

    </LinearLayout>

    <View
        android:layout_width="100px"
        android:layout_height="50px"
        android:background="@color/red" />

</LinearLayout>

The effect:

How to change the code below to achieve the effect above?
ps: If I specified the width of the vertical class, it works, is there another way to achieve this?

<html>
  <header>
    <style>

      .container {
        width: 400px;
        height: 200px;
        display: flex;
        flex-direction: row;
        align-items: center;
        background: teal;
      }

      .vertical {
        display: flex;
        /* width: 100px; I don't want to specify the width*/
        flex: 1;
        flex-direction: column;
      }

      p {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }

      .button {
        width: 100px;
        height: 50px;
        background: red;
      }

    </style>
  </header>

  <body>
    <div class="container">

      <div class="vertical">
        <p>TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT</p>
        <p>TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT</p>
      </div>

      <div class="button">

      </div>
    </div>
  </body>
</html>

回答1:


You need to add min-width: 0; to the .vertical element

<html>
  <header>
    <style>

      .container {
        width: 400px;
        height: 200px;
        display: flex;
        flex-direction: row;
        align-items: center;
        background: teal;
      }

      .vertical {
        display: flex;
        min-width: 0;
        flex: 1;
        flex-direction: column;
      }

      p {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }

      .button {
        width: 100px;
        height: 50px;
        background: red;
      }

    </style>
  </header>

  <body>
    <div class="container">

      <div class="vertical">
        <p>TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT</p>
        <p>TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT</p>
      </div>

      <div class="button">

      </div>
    </div>
  </body>
</html>

https://codepen.io/withn/pen/RwwOmKm

The issue has to do with the way text is wrapped in flexbox children.

More info here: https://css-tricks.com/flexbox-truncated-text/



来源:https://stackoverflow.com/questions/58990667/implement-the-android-linearlayout-weight-effect-via-flexbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!