下面是短信界面的布局文件, 当然是我仿三星之后的写信息界面的布局, 我们以此为例, 来谈谈layout_weight布局的一种用法。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gray_background"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/writemsg_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/title_background_style1"
android:orientation="horizontal" >
<ImageView
android:id="@+id/left_direction_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:src="@drawable/left_dirction_arrow" />
<ImageView
android:id="@+id/sms_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@id/left_direction_arrow"
android:src="@drawable/ic_launcher_smsmms" />
<TextView
android:id="@+id/new_sms_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dip"
android:layout_toRightOf="@id/sms_icon"
android:text="@string/new_message"
android:textSize="20sp"
android:typeface="serif" />
<TextView
android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical" />
<TextView
android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dip"
android:layout_toLeftOf="@id/right_text"
android:ellipsize="end"
android:singleLine="true" />
</RelativeLayout>
<LinearLayout
android:id="@+id/recipients_subject_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:visibility="gone" >
<LinearLayout
android:id="@+id/recipients_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip" >
<ViewStub
android:id="@+id/recipients_editor_stub"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout="@layout/recipients_editor" />
<LinearLayout
android:id="@+id/contacts_linear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/contacts_import"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginLeft="5dip"
android:layout_weight="1.0"
android:background="@xml/addcontacts_selector" />
</LinearLayout>
</LinearLayout>
<EditText
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:capitalize="sentences"
android:autoText="true"
android:singleLine="true"
android:maxLength="42"
android:hint="@string/subject_hint"
android:visibility="gone"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:background="@xml/recipients_sector"
android:paddingLeft="5dip"
android:textColor="#ffffffff"
android:textSize="20sp"
/>
</LinearLayout>
<!-- start --> 我们从这里来开始。 大家注意。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"我曾经范了一个致命的错误,在这里我用了wrap_content. 后来发现我的底盘居然不显示在底部, 搞得我痛苦了两三天, 为什么呢, 就是因为layout_weight这个属性。
android:orientation="vertical"
android:layout_gravity="bottom">这下面呢是一个布局, 里面主要有短信的底盘, 短信的listview,附件控件。
<view class="com.android.mms.ui.MessageListView"
style="?android:attr/listViewWhiteStyle"
android:id="@+id/history"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:listSelector="@drawable/chat_history_selector"
android:drawSelectorOnTop="true"
android:transcriptMode="alwaysScroll"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset"
android:stackFromBottom="true"
android:visibility="gone"
android:fadingEdge="none"
android:layout_marginBottom="1dip"
android:cacheColorHint="@android:color/white"
/>
我们来看上面和下面的红色部分, 这代表什么意思, 同在一个linearlayout布局里, 这表示
在父组件中, 先满 足下面的这个linearlayout的显示, 如果有空间, 再满 足上面的view组件的显示。
如果下面的短信附件,跟底盘部分把父组件显示满 了, 则隐藏上面的信息历史listview部分。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="0dip">
<view class="com.android.mms.ui.AttachmentEditor"
android:id="@+id/attachment_editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ViewStub
android:id="@+id/image_attachment_view_portrait_stub"
android:layout="@layout/image_attachment_view_portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/video_attachment_view_portrait_stub"
android:layout="@layout/video_attachment_view_portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/audio_attachment_view_portrait_stub"
android:layout="@layout/audio_attachment_view_portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/slideshow_attachment_view_portrait_stub"
android:layout="@layout/slideshow_attachment_view_portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/vcard_attachment_view_portrait_stub"
android:layout="@layout/vcard_attachment_view_portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/file_attachment_view_portrait_stub"
android:layout="@layout/file_attachment_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/image_attachment_view_landscape_stub"
android:layout="@layout/image_attachment_view_landscape"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/video_attachment_view_landscape_stub"
android:layout="@layout/video_attachment_view_landscape"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/audio_attachment_view_landscape_stub"
android:layout="@layout/audio_attachment_view_landscape"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/slideshow_attachment_view_landscape_stub"
android:layout="@layout/slideshow_attachment_view_landscape"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:id="@+id/vcard_attachment_view_landscape_stub"
android:layout="@layout/vcard_attachment_view_landscape"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</view>
</ScrollView>
同上面的理, 也是先满中短信底盘的显示, 如果还剩下空间, 就显示附件部分,那么我们到了这里会发现, 这个xml布局文件的逻辑,
那就是先满足底盘显示, 有空间了再满 足附件显示, 再有空间了就满 足信息历史listview的显示。
好了我们回过头来分析上面黄色部分的代码android:layout_height="match_parent
如果你将match_parent一不小改成了wrap_content. 那这个效果就是当没有信息历史, 没有附件时, 这个底盘就浮上去了, 下面留下一大片空白
我遇到这个问题是相当的烦, 我本来已经仿好了三星这个界面, 就是因为不小心改到了它。 结果出现大BUG, 找了几天啊, 真是烦死我了, 总算解决了。分享给大家, 希望大家以后不范这个错误。
<LinearLayout
android:id="@+id/bottom_panel"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:background="@drawable/bottombar_landscape_565">
<EditText
android:id="@+id/embedded_text_editor"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:autoText="true"
android:capitalize="sentences"
android:nextFocusRight="@+id/send_button"
android:hint="@string/type_to_compose_text_enter_to_send"
android:maxLines="9"
android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
android:imeOptions="actionDone|flagNoEnterAction"
android:minLines="2"
style="@style/smsmms_edittext_style"
android:gravity="top"
android:paddingLeft="5dip"
android:paddingTop="8dip"
android:textColor="#ffffffff"
android:textSize="20sp"
/>
<RelativeLayout
android:id="@+id/button_with_counter"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button android:id="@+id/add_attachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@xml/attach_button_style"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dip"
/>
<TextView android:id="@+id/buttondivider"
android:layout_width="0px"
android:layout_height="8px"
android:layout_above="@id/add_attachment"
/>
<Button
android:id="@+id/send_button"
android:layout_width="49px"
android:layout_height="40px"
android:nextFocusLeft="@+id/embedded_text_editor"
android:background="@xml/send_button_style"
android:layout_centerHorizontal="true"
android:layout_above="@id/buttondivider"
/>
<TextView
android:id="@+id/text_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="11sp"
android:textStyle="bold"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_above="@id/send_button"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- end -->
</LinearLayout>
图呢是仿好了之后的界面图, 中间出错的图啊, 我忘了抓 取了。