[英]Views are overflowed or disappeared in Android 5
如您在我的屏幕快照中所見,諸如EditText的視圖被溢出或消失。 這就是為什么我看不到多行EditText右側的滾動條的原因。
我該如何解決? 我嘗試了match_parent和fill_parent,但是看不到任何區別。 “ wrap_content”不適用於表單。
這是我的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.