简体   繁体   English

RelativeLayout,ScrollView和底部的导航栏

[英]RelativeLayout, ScrollView and navigation bar at bottom

What I want to do is, make layout like this: 我想要做的是,使这样的布局:

Title 标题

Date 日期

Long text with scrolling 滚动的长文本

Navigation bar stick to the bottom 导航栏粘在底部

Well I have done everything, however there is a little problem with scrolling. 好吧,我已经做了一切,但滚动有一点问题。 I want only to scroll text. 我只想滚动文字。 Title and date should be stick to the top, and nav bar to the bottom of activity. 标题和日期应该贴在顶部,导航栏应该放在活动的底部。 And yes, it works, but my nav bar overlaps text :/ 是的,它可以工作,但我的导航栏重叠文本:/

I tried everything, there is one solution I found, set fixed height for Scrollview, but this will not work on every devices well, isn't it? 我尝试了一切,我发现有一个解决方案,为Scrollview设置固定高度,但这不适用于每个设备,不是吗? I probably could do some calculation in code, and on it change height, but I would like to stay in XML. 我可能可以在代码中进行一些计算,并在其上改变高度,但我想保留XML。

Any one have any suggestions? 有人有什么建议吗?

Here is my XML file: 这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/feed_title"
                style="@style/h1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical" />

            <TextView
                android:id="@+id/feed_info"
                style="@style/h2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <ImageView
            android:id="@+id/feed_fav_ico"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical|right"
            android:background="@drawable/ic_fav_off" />
    </LinearLayout>


    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollY="20dp" >

        <TextView
            android:id="@+id/feed_text"
            style="@style/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Loren ipsum full tekst" />
    </ScrollView>
</LinearLayout>

<!-- Buttons -->

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:paddingBottom="5dp" >

    <Button
        android:id="@+id/go_to_article"
        style="@style/button_screen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="15dp"
        android:text="@string/feed_show_full" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/next_feed"
            style="@style/button_screen"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/button_arrow_up" />

        <Button
            android:id="@+id/share_feed"
            style="@style/button_screen"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="@string/feed_share" />

        <Button
            android:id="@+id/delete_feed"
            style="@style/button_screen"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="@string/feed_delete" />

        <Button
            android:id="@+id/prev_feed"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/button_arrow_down" />
    </LinearLayout>
</LinearLayout>
<!-- ~Buttons -->

</RelativeLayout>

Here are the things I have changed: 以下是我改变的事情:

  • Moved top layout to bottom 将顶部布局移至底部
  • Gave bottom layout name android:id="@+id/bottom_layout" 给底部布局名称android:id="@+id/bottom_layout"
  • Gave top layout a name android:id="@+id/top_layout" (Not necessary just for clarity) 给顶部布局一个名字android:id="@+id/top_layout" (为了清楚起见,没有必要)
  • Now top layout will have these properties: 现在顶部布局将具有以下属性:

    android:layout_above="@id/bottom_layout"

    android:layout_alignParentTop="true"

    The first one is to make top layout anchored above bottom layout. 第一个是将顶部布局固定在底部布局之上。 Second one is to align top edge of top layout to parent's top. 第二个是将顶部布局的顶部边缘与父级顶部对齐。 Which in this case is RelativeLayout. 在这种情况下,RelativeLayout。

  • Now bottom layout will have these properties: 现在底部布局将具有以下属性:

    android:layout_alignParentBottom="true"

    It will tell that bottom edge of bottom layout matches with bottom edge of parent (which is RelativeLayout) 它会告诉底部布局的底部边缘与父级的底边(即RelativeLayout)匹配

Below is the fully working layout: 以下是完全正常工作的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- Buttons -->

<LinearLayout
    android:id="@+id/bottom_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:paddingBottom="5dp" >

    <Button
        android:id="@+id/go_to_article"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="15dp"
        android:text="Feed full" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/next_feed"
            android:layout_width="40dp"
            android:layout_height="40dp" />

        <Button
            android:id="@+id/share_feed"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="share" />

        <Button
            android:id="@+id/delete_feed"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="delete" />

        <Button
            android:id="@+id/prev_feed"
            android:layout_width="40dp"
            android:layout_height="40dp" />
    </LinearLayout>
</LinearLayout>
<!-- ~Buttons -->

<LinearLayout
    android:id="@+id/top_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/bottom_layout"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/feed_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical" />

            <TextView
                android:id="@+id/feed_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <ImageView
            android:id="@+id/feed_fav_ico"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical|right"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollY="20dp" >

        <TextView
            android:id="@+id/feed_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/long_test" />
    </ScrollView>
</LinearLayout>

</RelativeLayout>

Did you try to put a weight to the scroll layout ? 你是否试图对滚动布局加权?

LinearLayout
- LinearLayout
-- Title
-- Date
- ScrollView layout_weigth=1
-- TextView
- LinearLayout
-- Button
-- Button
-- Button

Drag the bottom edge of the LinearLayout (lets call it l1) holding your text and align it to the top edge of the linear layout holding your navigation bar (let's call it l2). 拖动LinearLayout的下边缘(让我们称之为l1),保持文本并将其对齐到保持导航栏的线性布局的上边缘(让我们称之为l2)。 So that the AbsoluteLayout.above on l1 is equal to l2. 这样l1上的AbsoluteLayout.above等于l2。

<LinearLayout android:id="@+id/l1"
    android:layout_above="@+id/l2">
</LinearLayout>

Like Jonas says, but to give you a litte more, make sure you fill in the other stuff. 就像乔纳斯说的那样,但是为了给你更多的东西,请确保你填写其他东西。

<LinearLayout 
    android:layout_height="fill-parent">

    <LinearLayout 
       android:layout_height="wrap_content">
       put your title and things in here
    </LinearLayout>

    <ScrollView
       android:layout_height="fill_parent"
       android:layout_weight="1">  <!-- this makes it not overlap the layout(navbar) below-->
       put your title and things in here
    </ScrollView>

    <LinearLayout
       android:layout_height="wrap_content">
       put your nav bar stuff in here
    </LinearLayout>
</LinearLayout>

If you do not set the layout_weight, the scrollview will actually push the navbar out off the bottom of the screen in this configuration. 如果未设置layout_weight,则滚动视图实际上会在此配置中将导航栏从屏幕底部推出。 It has something to do with how the space for layouts is reserved, I'm not completely sure why, but adding weight delays the reservation till later. 它与如何保留布局空间有关,我不完全确定原因,但增加重量会延迟预订直到以后。 Since the scrollview is filling the parent but still is part of the linear layout, the layout will make sure to accomodate your lower navbar, and unlike the Relative Layout there will be no overlap. 由于scrollview正在填充父级但仍然是线性布局的一部分,因此布局将确保容纳较低的导航栏,并且与相对布局不同,将不会重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM