【问题标题】:Layout preview and actual device do not match布局预览与实际设备不匹配
【发布时间】:2017-02-02 03:32:39
【问题描述】:

我不知道为什么会这样。我敢打赌这很简单,但我不知道为什么。我有一个评论活动,其中一个相对布局作为父级,一个 RecyclerView 以及一个线性布局(有 2 个子级:一个编辑文本和一个提交按钮)作为子级。我的 layout.xml 如下

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.erik.appname.CommentActivity">


<android.support.v7.widget.RecyclerView
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:id="@+id/posted_comments">

</android.support.v7.widget.RecyclerView>



<LinearLayout
    android:id="@+id/write_comment"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/posted_comments"
    android:layout_alignParentStart="true">

    <EditText
        android:layout_alignParentBottom="true"
        android:paddingLeft="10dp"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:background="@drawable/input_outline2"
        android:layout_height="match_parent"
        android:inputType="textPersonName|textMultiLine"
        android:text=""
        android:gravity="left|center"
        android:hint="Comment..."
        android:textSize="20dp"
        android:maxLength="100"
        android:id="@+id/current_comment" />

    <ImageButton
        android:src="@drawable/submit"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="@drawable/input_outline2"
        android:layout_alignParentBottom="true"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/submit"
        android:layout_toEndOf="@+id/current_comment"
        />


</LinearLayout>

这是我从预览中得到的(我希望它看起来像) preview

虽然这是来自实际设备的屏幕截图real device

为什么recyclerview下方的edittext和按钮会这样?

我尝试了很多事情,例如将所有内容包装在线性布局中,将 RelativeLayout 包装在线性布局中,将包含编辑文本和按钮的线性布局包装在另一个中,并尝试将它们堆叠起来并使用 layout_weight 来确定比例。 .. 似乎无法找出正确的方法。

非常感谢您的帮助!

【问题讨论】:

    标签: xml android-layout android-linearlayout android-relativelayout xml-layout


    【解决方案1】:

    您已将RecyclerView 的高度指定为android:layout_height="470dp",该高度仅限于470dp,并且在采用预定义的高度后,其余部分将留给底部LinearLayout。定义硬代码高度不是一个好主意。尝试删除RecyclerViewmatch_parentheightwidth就可以了。

    已解决的代码 [使用您的下一个值编辑]:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_comment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/posted_comments"
        android:layout_above="@+id/write_comment">
    
    </android.support.v7.widget.RecyclerView>
    
    
    <LinearLayout
        android:id="@+id/write_comment"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true">
    
        <EditText
            android:paddingEnd="10dp"
            android:layout_width="0dp"
            android:layout_weight="8"
            android:background="@mipmap/ic_launcher"
            android:layout_height="match_parent"
            android:inputType="textPersonName|textMultiLine"
            android:text=""
            android:gravity="left|center"
            android:hint="Comment..."
            android:textSize="20sp"
            android:maxLength="100"
            android:id="@+id/current_comment" />
    
        <ImageButton
            android:src="@mipmap/ic_launcher"
            android:scaleType="fitCenter"
            android:padding="5dp"
            android:background="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:id="@+id/submit"
            android:layout_toEndOf="@+id/current_comment"
            />
    
    
    </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

    • 太棒了!我尝试过投票,但不幸的是,在我这样做之前我还有一些代表要做。感谢先生的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多