简体   繁体   English

视图在Android 5中溢出或消失

[英]Views are overflowed or disappeared in Android 5

As you can see in my screenshot, views like an EditText are overflowed or disappeared. 如您在我的屏幕快照中所见,诸如EditText的视图被溢出或消失。 That's why I can't see the scrollbar on the right side of a multiline EditText. 这就是为什么我看不到多行EditText右侧的滚动条的原因。

How can I fix that? 我该如何解决? I tried match_parent and fill_parent, but can't see a difference. 我尝试了match_parent和fill_parent,但是看不到任何区别。 'wrap_content' isn't nice for a form. “ wrap_content”不适用于表单。

Screenshot Android Studio, EditText overflows 屏幕截图Android Studio,EditText溢出

Here is my xml: 这是我的xml:

<?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="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2">

        <TextView android:text="Label for first EditText" />

        <EditText
            android:id="@+id/et_id"
            android:layout_width="match_parent"
            android:lines="1"
            android:maxLength="13"
            android:maxLines="1" />

        <TextView android:text="Another Label for another ET" />

        <EditText
            android:id="@+id/et_new_description"
            android:layout_width="match_parent"
            android:lines="3"
            android:maxLines="10"
            android:inputType="textMultiLine"
            android:scrollbars="vertical" <!-- can't see this -->
            android:scrollbarAlwaysDrawVerticalTrack="true"/>

    </GridLayout>

    <android.support.v7.widget.ButtonBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableLeft="@drawable/filter"
            android:text="cancel" />

        <Button
            android:id="@+id/button_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableLeft="@android:drawable/ic_menu_save"
            android:text="save" />

    </android.support.v7.widget.ButtonBarLayout>
</RelativeLayout>

I think the proper way to accomplish this is with the android:layout_columnWeight attribute. 我认为完成此操作的正确方法是使用android:layout_columnWeight属性。 Here is a sample: 这是一个示例:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    ... >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:text="Label"
        ... />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnWeight="1"
        ... />

    <!-- Same pattern for other labels and text fields -->

</GridLayout>

However, this attribute is only available starting in API 21 (Lollipop). 但是,此属性仅从API 21(Lollipop)开始可用。 You could use the support version of GridLayout instead, which requires you to add 您可以改用GridLayout的支持版本,这需要您添加

compile 'com.android.support:gridlayout-v7:23.3.0'

to the dependencies block in your build.gradle file, and then use build.gradle文件中的dependencies块,然后使用

<android.support.v7.widget.GridLayout>

in your layout file. 在您的布局文件中。 This is what I would recommend to you. 这就是我向您推荐的。


If you want to support versions older than Lollipop without also using the support version of GridLayout, here's another way to accomplish the effect you want: 如果要支持早于Lollipop的版本而不使用GridLayout的支持版本,则这是另一种实现所需效果的方法:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    ... >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:text="Label"
        ... />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_gravity="fill_horizontal"
        ... />

    <!-- Same pattern for other labels and text fields -->

</GridLayout>

You might have to use android:layout_width="wrap_content" instead of android:layout_width="0dp" for the EditText , I'm not sure. 您不确定EditText可能需要使用android:layout_width="wrap_content"而不是android:layout_width="0dp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM