繁体   English   中英

视图在Android 5中溢出或消失

[英]Views are overflowed or disappeared in Android 5

如您在我的屏幕快照中所见,诸如EditText的视图被溢出或消失。 这就是为什么我看不到多行EditText右侧的滚动条的原因。

我该如何解决? 我尝试了match_parent和fill_parent,但是看不到任何区别。 “ wrap_content”不适用于表单。

屏幕截图Android Studio,EditText溢出

这是我的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>

我认为完成此操作的正确方法是使用android:layout_columnWeight属性。 这是一个示例:

<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>

但是,此属性仅从API 21(Lollipop)开始可用。 您可以改用GridLayout的支持版本,这需要您添加

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

build.gradle文件中的dependencies块,然后使用

<android.support.v7.widget.GridLayout>

在您的布局文件中。 这就是我向您推荐的。


如果要支持早于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>

您不确定EditText可能需要使用android:layout_width="wrap_content"而不是android:layout_width="0dp"

暂无
暂无

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

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