Android LinearLayout填充宽度

我想知道为什么会这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:gravity="center_vertical"
    android:layout_height="wrap_content" android:layout_gravity="top"
    android:background="@drawable/gradient" android:focusable="true"
    android:paddingTop="5dip" android:paddingBottom="5dip"
    android:focusableInTouchMode="true">
        <EditText android:id="@+id/searchField"
            android:layout_width="wrap_content" android:layout_height="fill_parent"

            android:paddingTop="2dip" android:paddingBottom="2dip"
            android:paddingLeft="10dip" android:paddingRight="10dip"
            android:inputType="textVisiblePassword" android:radius="5dip"
            android:layout_marginLeft="10dip"
            android:background="@drawable/search_background" android:textColor="#000000" />

        <Button android:id="@+id/info" android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:background="@drawable/info_btn" />
</LinearLayout>

如果我将“fill_parent”设置为我的EditText的layout_width,它将占用父级的整个宽度,按钮消失(我猜它隐藏在EditText下面).

任何人都可以解释一下吗?我想让Button获得自己的宽度,EditText获取剩余的宽度.

解决方法

你告诉EditText到fill_parent,因此它填满了屏幕(整个宽度,没有为你的按钮留出空间).

您希望按钮包装其内容和编辑文本以填充余数,您可以使用layout_weight属性执行此操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="top"
 android:gravity="center_vertical"
 android:paddingTop="5dip"
 android:paddingBottom="5dip"
 android:orientation="horizontal">
<EditText
    android:id="@+id/searchField"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_marginLeft="10dip"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:radius="5dip"
    android:inputType="textVisiblePassword"
    android:textColor="#000000" />
<Button
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:text="Blah" />
</LinearLayout>

(我把你的可绘制参考文献用于其他人的通用)

另一方面,不要让你的XML在eclipse中看起来更整洁,更易于维护:

Window > Preferences > XML > XML Files > Editor

Line width 120

Split multiple attributes TICK

Align final bracket UNTICK

Preserve Whitespace tags in PCDATA UNTICK

Clear all blank lines UNTICK

Insert Whitespace before closing TICK

Click ok

然后你可以修复你的XML文件,当你打开一个XML文件CTRL A然后CTRL SHIFT F将很好地格式化它!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...