[英]Scroll View in Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E1E1E"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 3"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="Botton 2"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 3"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 4"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 5"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 6"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="#fff"
android:weightSum="6" >
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/b1"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/home1"
android:text="Home"
android:textSize="8dp" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b2"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/abt"
android:text="About"
android:textSize="8dp" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b3"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/ser"
android:text="Services"
android:textSize="8dp" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b4"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/cnt1"
android:text="Portfolio"
android:textSize="8dp" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b5"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/spt"
android:text="Support"
android:textSize="8dp" />
<Button
android:id="@+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b6"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/inbox"
android:text="Marketing"
android:textSize="8dp" />
</LinearLayout>
</RelativeLayout>
我需要在頁腳固定的相對布局中使用滾動視圖。 僅表格布局應具有滾動視圖,頁腳應固定
將滾動視圖添加為TableLayout的父級,並將其在底部布局上方對齊。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E1E1E" >
<LinearLayout
android:id="@+id/bottombar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="#fff"
android:weightSum="6" >
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/b1"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/home1"
android:text="Home"
android:textSize="8dp" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b2"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/abt"
android:text="About"
android:textSize="8dp" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b3"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/ser"
android:text="Services"
android:textSize="8dp" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b4"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/cnt1"
android:text="Portfolio"
android:textSize="8dp" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b5"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/spt"
android:text="Support"
android:textSize="8dp" />
<Button
android:id="@+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/b6"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="@drawable/inbox"
android:text="Marketing"
android:textSize="8dp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottombar" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 3"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="Botton 2"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 3"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 4"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:weightSum="1" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ld"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 5"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Button 6"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
將其用作頂層,並添加所需的內容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context=".MainActivityFragment">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="horizontal"
android:id="@+id/footer">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
在給定的xml代碼中,添加滾動視圖並替換其中的TableLayout。 這樣,讓它成為第一個布局。
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E1E1E">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//contents inside table layout
</TableLayout>
</ScrollView>
</RelativeLayout>
現在創建另一個頁腳布局,像這樣,讓它成為第二個布局。
<RelativeLayout>
//things to be appeared on footer are added here
</RelativeLayout>
在您的Java代碼中,使用它們的ID獲取兩個布局,將第二個布局設置為第一個布局的頁腳。
firstlayout.addFooterView(secondlayout);
現在,您可以獲得固定的頁腳,並且僅在第一個layouts表布局中滾動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.