我是Android编程的新手。我尝试设计聊天布局,但我发现了布局设计的一些问题。为什么编辑文本布局不会在横向模式下自动拉伸?
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/myappbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<include
layout="@layout/input_message_area"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_weight="1"
android:gravity="bottom" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/myappbar"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="75dp"
android:background="#63DDEC"
android:paddingLeft="5dip"
android:paddingTop="0dip"
android:paddingRight="5dip"
android:paddingBottom="1dip">
<ListView
android:id="@+id/lv_chat_story"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="@color/colorTransparent"
android:dividerHeight="0dp" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
这适用于 input_message_area.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#63DDEC"
android:gravity="bottom"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/inputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_a"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="5dp"
android:padding="3dp">
<Button
android:id="@+id/buttonEmoji"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/smile_center" />
<EditText
android:id="@+id/chat_edit_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:scrollHorizontally="false"
android:layout_toRightOf="@id/buttonEmoji"
android:hint="Type a message"
android:maxLines="4"
android:singleLine="false"
android:inputType="textCapSentences"
android:textSize="18sp"
android:paddingLeft="4dp"/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chat_edit_text1"
android:background="@drawable/camera_center"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_main"
android:layout_toRightOf="@id/inputLayout"
android:layout_alignBaseline="@+id/inputLayout"
android:layout_alignBottom="@+id/inputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-10dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/send_center"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/inputLayout"
app:layout_anchorGravity="bottom|right" />
</LinearLayout>
谢谢。
答案 0 :(得分:2)
为RelativeLayout添加重量。 请找我的代码。这对你有帮助。
<强> input_message_area.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#63DDEC"
android:gravity="bottom"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/inputLayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_a"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="5dp"
android:padding="3dp">
<Button
android:id="@+id/buttonEmoji"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/smile_center" />
<EditText
android:id="@+id/chat_edit_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:scrollHorizontally="false"
android:layout_toRightOf="@id/buttonEmoji"
android:hint="Type a message"
android:layout_marginRight="50dp"
android:maxLines="4"
android:singleLine="false"
android:inputType="textCapSentences"
android:textSize="18sp"
android:paddingLeft="4dp"/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/camera_center"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_main"
android:layout_toRightOf="@id/inputLayout"
android:layout_alignBaseline="@+id/inputLayout"
android:layout_alignBottom="@+id/inputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-10dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/send_center"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/inputLayout"
app:layout_anchorGravity="bottom|right" />
答案 1 :(得分:1)
将android:layout_width="match_parent"
提供给Edit Text
。
<EditText
android:id="@+id/chat_edit_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:scrollHorizontally="false"
android:layout_toRightOf="@id/buttonEmoji"
android:hint="Type a message"
android:maxLines="4"
android:singleLine="false"
android:inputType="textCapSentences"
android:textSize="18sp"
android:paddingLeft="4dp"/>
答案 2 :(得分:0)
将EditText宽度从wrap_content
更改为match_parent
。
<强> input_message_area.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#63DDEC"
android:gravity="bottom"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/inputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_a"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="5dp"
android:padding="3dp">
<Button
android:id="@+id/buttonEmoji"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/smile_center" />
<EditText
android:id="@+id/chat_edit_text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:scrollHorizontally="false"
android:layout_toRightOf="@id/buttonEmoji"
android:hint="Type a message"
android:maxLines="4"
android:singleLine="false"
android:inputType="textCapSentences"
android:textSize="18sp"
android:paddingLeft="4dp"/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chat_edit_text1"
android:background="@drawable/camera_center"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_main"
android:layout_toRightOf="@id/inputLayout"
android:layout_alignBaseline="@+id/inputLayout"
android:layout_alignBottom="@+id/inputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-10dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/send_center"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/inputLayout"
app:layout_anchorGravity="bottom|right" />
</LinearLayout>