底部的RelativeLayout、ScrollView和导航栏
我想要做的是,使布局像这样:
标题
日期
带有滚动导航栏的长文本
粘在底部
好吧,我已经完成了所有操作,但是滚动有一个小问题。我只想滚动文本。标题和日期应位于活动的顶部,导航栏应位于活动的底部。是的,它可以工作,但是我的导航栏与文本重叠:/
我尝试了一切,我找到了一个解决方案,为 Scrollview 设置固定高度,但这并不适用于所有设备,不是吗?我可能可以在代码中进行一些计算,并在其上更改高度,但我想留在 XML 中。
有人有什么建议吗?
这是我的 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>
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? I probably could do some calculation in code, and on it change height, but I would like to stay in XML.
Any one have any suggestions?
Here is my XML file:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是我更改的内容:
android:id="@+id/bottom_layout"
android:id="@+ id/top_layout"
(为了清楚起见,没有必要)现在顶部布局将具有以下属性:
android:layout_above="@id/bottom_layout"
android:layout_alignParentTop="true"
第一个是将顶部布局锚定在底部布局之上。
第二个是将顶部布局的顶部边缘与父级的顶部对齐。在本例中是RelativeLayout。
现在底部布局将具有以下属性:
android:layout_alignParentBottom="true"
它会告诉底部布局的底部边缘与父级(即RelativeLayout)的底部边缘匹配
下面是完整的工作布局:
Here are the things I have changed:
android:id="@+id/bottom_layout"
android:id="@+id/top_layout"
(Not necessary just for clarity)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.
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)
Below is the fully working layout:
您是否尝试过对滚动布局施加权重?
Did you try to put a weight to the scroll layout ?
拖动包含文本的 LinearLayout(我们称之为 l1)的底部边缘,并将其与包含导航栏的线性布局的顶部边缘(我们称之为 l2)对齐。这样l1上的AbsoluteLayout.above就等于l2。
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). So that the AbsoluteLayout.above on l1 is equal to l2.
就像乔纳斯说的那样,但为了给你更多的信息,请确保你填写了其他内容。
如果您不设置layout_weight,则在此配置中滚动视图实际上会将导航栏推出屏幕底部。这与如何保留布局空间有关,我不完全确定为什么,但增加重量会将保留推迟到以后。
由于滚动视图填充了父级,但仍然是线性布局的一部分,因此该布局将确保容纳您的下部导航栏,并且与相对布局不同,不会有重叠。
Like Jonas says, but to give you a litte more, make sure you fill in the other stuff.
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. 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.