展会信息港展会大全

使用FrameLayout替代AbsoluteLayout android开发教程
来源:互联网   发布日期:2015-10-13 12:45:49   浏览:2645次  

导读:有时需要将某个视图定位在另外视图的旁边,比如类似ios的popover那样。这时候想到的就是用绝对布局,即AbsoluteLayout,但这是Android sdk设计者不推荐的:视图如果是定位到其他视图的旁边,或者父视图的相对的......

有时需要将某个视图定位在另外视图的旁边,比如类似ios的popover那样。这时候想到的就是用绝对布局,即AbsoluteLayout,但这是Android sdk设计者不推荐的:

视图如果是定位到其他视图的旁边,或者父视图的相对的位置(比如父视图的底部),这些可以用RelativeLayout代替。但是如果是以下情况,可以考虑用FrameLayout:

想参照的视图(不是父视图)是动态生成的,那么就没有id,就不能通过RelativeLayout的addrule方法定位

需要将视图精确定位到某个坐标上

如果不修改布局参数,使用FrameLayout布局,显示两段TextView,则会叠加在一起:

布局的xml文件:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/mainlayout"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="@string/hello" />

<TextView android:id="@+id/pomptText" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="你好,世界!" />

</FrameLayout>

通过修改,可以实现类似这样的效果:

在布局文件中:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/mainlayout"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="@string/hello" />

<TextView android:id="@+id/pompt" android:layout_width="90dp"

android:layout_height="28dp" android:text="你好,世界!" android:background="#ff0000ff"

android:layout_gravity="center" android:padding="1dp"/>

</FrameLayout>

注意加粗的那个属性。是让视图在布局中居中。然后这样调整:

View view = this.findViewById(R.id.pompt);

FrameLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();

params.leftMargin = -170;

params.topMargin = -300;

view.setLayoutParams(params);

设置到指定的位置。注意,居中的位置是坐标原点。因此设置的值都是负值。在取值的时候,要做运算。根据中点,计算出视图的左上坐标,再计算它到制定位置的位移偏移量,也就是中点的位移值。

这时如果touch到第一行文本,接收事件的就是该文本视图对象:

touch第二行文本,情况类似:

touch其他地方,则FrameLayout布局将接收touch事件:

这样可能不是我们想要的效果。比如希望touch第二行文本以外的部分将触发该文本视图隐藏。那么可以在第二行文本视图之外增加一个FrameLayout:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/mainlayout"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="@string/hello" />

<FrameLayout android:id="@+id/promptLayout"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:id="@+id/prompt" android:layout_width="90dp"

android:layout_height="28dp" android:text="你好,世界!"

android:background="#ff0000ff" android:layout_gravity="center"

android:padding="1dp" />

</FrameLayout>

</FrameLayout>

这样效果一样,而touch第二行文本视图以外,则到达新增的FrameLayout上:

赞助本站

人工智能实验室

相关热词: FrameLayout AbsoluteLayout

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港