Android实现滑动图片(ViewPager)学习之一:布局

这篇博客详细解读了一个Android滑动图片的布局实现,涉及LinearLayout、RelativeLayout和FrameLayout的使用。LinearLayout提供水平和垂直布局,RelativeLayout依赖于其他控件定位,而FrameLayout则允许控件叠加。布局示例中,外层是垂直LinearLayout,包含一个RelativeLayout(用于标题栏)和一个FrameLayout(内含ViewPager和底部控件)。ViewPager占据整个FrameLayout显示滑动图片,底部是LinearLayout,展示文本和控制按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们来解读一下布局文件,这个布局文件对于初学来说是个相对比较复杂的布局

在这个布局文件中主要用到了三种layout,分别是LinearLayoutRelativeLayoutFrameLayout


简单说一下,

LinearLayout是一个线性布局,分为水平和垂直两种布局,这种布局方式就好像画直线一样

RelativeLayout是一个相对布局,容器中的控件的位置依赖于其它控件或者父控件定位,布局方式类似于web前端中css+div的布局方式

FrameLayout布局方式,有点像汉诺塔,布局中的控件是一个叠着一个摆放的


 

下面是整个demo运行界面的截图:

整个界面外层是一个垂直的LinearLayout,嵌套了一个RelativeLayout和一个FrameLayout


 



RelativeLayout主要是实现标题栏的布局。

在这个布局中ImageButton是个返回按钮,紧接着是两个宽为1pxview用来绘制分割线,最右边是一个居中显示的TextView

 

 

  



FrameLayout中,下层是一个android.support.v4.view.ViewPager控件,这个控件占满整个FrameLayout,用于显示滑动的图片

上层是一个居下显示(layout_gravity="bottom")的LinearLayout用于盛放文本和控制按钮

在这个LinearLayout中,有两部分一部分是一个TextView控件,用于显示文本;在这个控件下又是一个水平的Linearlayout,在这个水平的Linearlayout中并列排放了五个view(控制按钮)

 



 这就是整个布局文件的内容,下面附上布局文件的代码

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="@drawable/title_bk" >

        <ImageButton
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_back_selector"
            android:src="@drawable/btn_back" />

        <View
            android:id="@+id/line0"
            android:layout_width="1px"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/btn_back"
            android:background="#aa11264f" />

        <View
            android:layout_width="1px"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/line0"
            android:background="#009ad6" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="优酷客户端"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="140dip" >

        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dip"
            android:layout_gravity="bottom"
            android:background="#33000000"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="中国家庭院校园区域名字体现"
                android:textColor="#ffffff" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dip"
                android:gravity="center" >

                <View
                    android:id="@+id/v_dot0"
                    style="@style/dot_style"
                    android:background="@drawable/dot_focused" />

                <View
                    android:id="@+id/v_dot1"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/v_dot2"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/v_dot3"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/v_dot4"
                    style="@style/dot_style" />
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值