我有一个带有textView的listView和按钮,这是我的布局内容
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/desc"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right">
<Button
android:id="@+id/button"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
</LinearLayout>
</FrameLayout>
这是一个屏幕截图,描述了错误:http://it.tinypic.com/r/a47ho9/9
我不会对ListView
行使用FrameLayout
,因为很难将项目定位在FrameLayout
中。只需将一个LinearLayout
与android:orientation="horizontal"
一起使用,然后将TextView
和Button
放入其中。
我会说,使用RelatiVelayout可以正确放置您的小部件。我还建议尝试使您的布局变平。这意味着您不应嵌套这么多布局。在这种情况下,这是一件非常简单的事情。在将UI构建为优化的性能时,您希望拥有尽可能少的嵌套布局。
尝试这样的事情,可能不是您想要的,但也许可以帮助您。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background=""
android:padding="8dp">
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium text"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toLeftOf="@+id/button"/>
<Button
android:id="@+id/button"
android:layout_width="70dp"
android:text="button"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
您可以在此处阅读有关优化布局的更多信息https://developer.android.com/training/improving-layouts/optimizing-layout.html