FrameLayout详解

先看演示:

 

FrameLayout框架布局是最简单的布局形式。所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。这种显示方式有些类似于堆栈。

当我们往里面添加组件的时候,所有的组件都会放置于这块区域的左上角;

帧布局的大小由子控件中最大的子控件决定,如果都组件都一样大的话,同一时刻就只能看到最上面的那个组件了。

1 layout_gravity

      FrameLayout根本无法控制他的子控件的位置,子控件可以通过android:layout_gravity属性来控制自己在父控件中的位置,从而制定组件的对其方式。

2 layout_margin

        FrameLayout布局里面的控件单独设置layout_margin类的属性没有效果。FrameLayout中的控件layout_margin设置要依赖layout_gravity属性,否则layout_margin设置无效。layout_gravity有好几个值可以设置,具体要设置哪一个呢?其实layout_gravity可以理解为设置控件的参考点,控件最终显示位置最终由layout_gravity和layout_margin共同决定。

  如果想要控件正常显示,可以将控件的layout_gravity设置为top,以屏幕左上角为参考点。

3 前景图像:

       永远处于帧布局最顶的,直接面对用户的图像,,就是不会被覆盖的图片

常用属性:

   android:foreground:设置该帧布局容器的前景图像

     android:foregroundGravity:设置前景图像显示的位置

帧布局在游戏开发方面用的比较多。当你需要自己写一个View的时候,在View里面已经完成了你的逻辑(例如游戏^_^),那么这个View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。

3应用实例

 activity代码

package mm.shandong.com.testframelayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class TestFrameLayoutActivity extends AppCompatActivity {
    FrameLayout frameLayout;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_frame_layout);
        btn = (Button) findViewById(R.id.btn);
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int x = (int) motionEvent.getX();
                int y = (int) motionEvent.getY();
                int width = btn.getWidth();
                int height = btn.getHeight();
                btn.layout(x, y, x + width, y + height);
                return false;
            }
        });
    }
}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面是一个FrameLayout,默认布局" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#ff0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一层"
            android:textColor="#00ff00"
            android:textSize="55sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二层"
            android:textColor="#0000ff"
            android:textSize="45sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三层"
            android:textColor="#00ffff"
            android:textSize="35sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第四层"
            android:textColor="#ffff00"
            android:textSize="25sp" />
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面是一个FrameLayout,通过layout_gravity和margin调节位置,并设置前景图片和背景颜色" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="#ff0000"
        android:foreground="@drawable/red"
        android:foregroundGravity="right|bottom">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|left"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="0dp"
            android:text="左上 left:20 Top:0"
            android:textColor="#00ff00"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|left"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="25dp"
            android:text="左上 left:20 Top:25"
            android:textColor="#00aa00"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|left"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="45dp"
            android:text="左下 left:45 Bottom:20"
            android:textColor="#0000ff"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="居中"
            android:textColor="#00ffff"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|top"
            android:layout_marginTop="30dp"
            android:text="右上 top 30"
            android:textColor="#ffff00"
            android:textSize="15sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="80dp"
            android:text="最上层,仍被遮盖"
            android:textSize="15sp" />
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面是一个FrameLayout,通过layout方法设置控件具体位置,请单击framelayou" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000">

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请点击别处" />
    </FrameLayout>
</LinearLayout>

  本人微博:honey_11

 Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:http://sj.qq.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy2,源码例子文档一网打尽

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHost 是 Android 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost 的使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值