Relativelayout,scrollview和toct
-
25-10-2019 - |
题
我想做的是这样做这样的布局:
标题
日期
滚动的长文字
导航杆粘在底部
好吧,我已经做了所有事情,但是滚动有一些问题。我只想滚动文字。标题和日期应粘贴在顶部,而导航栏则应延伸到活动的底部。是的,它有效,但是我的导航栏重叠文本:/
我尝试了所有东西,有一个解决方案,设置了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>
解决方案
这是我改变的事情:
- 将顶部布局移至底部
- 给出底部布局名称
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)
以下是完全工作的布局:
<?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>
其他提示
您是否尝试过重量滚动布局?
LinearLayout
- LinearLayout
-- Title
-- Date
- ScrollView layout_weigth=1
-- TextView
- LinearLayout
-- Button
-- Button
-- Button
拖动线性层的底部边缘(我们称其为l1),将其持有文本并将其对齐到持续导航栏的线性布局的顶部边缘(我们称其为l2)。因此,L1上的absolutelayout.above等于L2。
<LinearLayout android:id="@+id/l1"
android:layout_above="@+id/l2">
</LinearLayout>
就像乔纳斯(Jonas)所说的那样,但是要给您更多的小麻烦,请确保您填写其他内容。
<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>
如果您不设置Layout_Weeight,则ScrollView实际上将在此配置中将Navbar从屏幕底部推出。它与保留布局空间的保留空间有关,我不确定为什么,但是增加重量会延迟预订,直到以后。由于ScrollView填充了父,但仍然是线性布局的一部分,因此布局将确保可容纳您的较低的Navbar,并且与相对布局不同,不会有重叠。