【问题标题】:TextView and EditText cut off in LinearLayout在 LinearLayout 中的 TextView 和 EditText 被截断
【发布时间】:2015-05-28 00:15:50
【问题描述】:

我有一个水平方向的 LinearLayout,我在其中动态添加了一些 TextView 和 EditText。我的问题是当 TextView 很长时,它被切断(按高度)并且 EditText 是不可见的。

向视图添加填充并没有解决问题。设置 LinearLayout minHeight 值时,TextView 显示正确,但 EditText 仍然不可见。

<LinearLayout
        android:id="@+id/content_content"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"></LinearLayout>

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);

for (int i = 0; i < cntCount; i++) {
    TextView cntView = new TextView(getActivity());
    cntView.setLayoutParams(params);
    cntView.setText(cnt.get(i));

    contentContentView.addView(cntView);

    EditText answerView = new EditText(getActivity());
    answerView.setLayoutParams(params);
    answerView.setId(i);
    answerView.setHint("......");

    contentContentView.addView(answerView);

}

编辑

我找到了解决方案并创建了一个库。有兴趣的可以下载here

【问题讨论】:

    标签: android android-layout android-edittext android-view textview


    【解决方案1】:

    您的LinearLayout 具有“水平”方向 - 它会将下一个视图添加到当前视图的右侧。这意味着如果TextView 包裹(填充父级),则LinearLayout 中没有空间可以在屏幕上显示任何其他视图(它们将添加到可见布局的右侧)。

    如果您尝试将EditText 与一系列TextViews 放在同一行,那么您将需要创建自定义TextView 和布局。这需要对 EditText 的位置和大小进行测量,这不是 Android 标准的。

    这篇文章可能会有所帮助:How to overlay EditText on TextView just after the end of text

    【讨论】:

      【解决方案2】:

      您可以为 EditText 和 TextView 设置 layout_weights,而不是为 layout_width 使用 wrap_content。这样可以确保始终显示 EditText。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 2018-09-08
        相关资源
        最近更新 更多