【问题标题】:How to make Android LinearLayout to clip its children content?如何使 Android LinearLayout 剪辑其子内容?
【发布时间】:2017-10-29 17:37:38
【问题描述】:

我有一个 LinearLayout(水平),里面有几个子 TextView。我为 LinearLayout 和所有 TextViews 设置了固定的 layout_width。当孩子的总宽度超过布局的宽度时,它会自动缩小孩子,使它们变窄,这不是我想要的。我希望布局剪辑子内容并保持其宽度。 我怎样才能做到这一点?

例如:假设我的 LinearLayout 是 200dp 宽,并且有 4 个 TextView 每个 60dp 宽。我希望它显示前三个 TextView,以及第四个的 1/3(60 + 60 + 60 + 20 = 200)

谢谢。

【问题讨论】:

  • 粘贴您的代码

标签: android android-layout


【解决方案1】:
  • LinearLayout中使用android:weightSum

  • 并在TextView中使用android:layout_width="0dp"android:layout_weight="1"

试试这个。

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

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:text="hello"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:text="hello"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:text="hello"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="hello"/>

</LinearLayout>

还有

您可以在TextView 中将android:layout_width="match_parent" 更改为android:layout_width="wrap_content"

编辑

你可以在你的java代码中使用。

您可以自行更改宽度或重量。

private LinearLayout mLinearLayout;
private int viewsCount = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
    for (int i = 0; i < viewsCount; i++) {
        TextView textView = new TextView(this);
        if(i == viewsCount-1){
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        } else {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3);
        }
        mLinearLayout.addView(textView);
    }
}

【讨论】:

  • 您的第一个解决方案将显示前三个 TextView 并隐藏后续的。而且它仍然在前三个之间平均划分LinearLayout空间,这不是我的情况。我希望 LinearLayout 将我的 TextView 保持在指定的宽度,尽可能多地显示内容,剪裁其余部分。例如。假设我的 LinearLayout 是 200dp 宽,每个 60dp 宽有 4 个 TextView。我希望它显示前三个 TextView,以及第四个的 1/3。 p.s.我可以使用 RelativeLayout 和 ConstraintLayout 来做到这一点,但我想使用 LinearLayout。
  • 您可以为3,3,3,1设置权重。
  • 如果你不想平均最后一个textview。你可以使用&lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello"/&gt;
  • 如果您事先知道您的 TextView 集,这可能会起作用。但是,TextView 的数量以及它们的宽度在创建布局时是未知的。以后可以添加更多的 TextView。每个可能具有不同的宽度。您的意思是每次更新时我们都需要重新计算权重吗?
  • 是的,你可以使用
猜你喜欢
  • 1970-01-01
  • 2010-10-06
  • 2022-09-24
  • 2019-05-14
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
相关资源
最近更新 更多