繁体   English   中英

Android应用程序中的浮动按钮(分页视图)

[英]Floating button in Android application (Paging View)

我有一个iPhone应用程序,可以浏览一系列图像。 我使用以下代码在我的视图控制器中放置一个浮动帮助按钮,该按钮与用户浏览图像时在屏幕上的位置相同:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Help.png"] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(viewHelp)
     forControlEvents:UIControlEventTouchDown];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        button.frame = CGRectMake(60.0, 60.0, 210.0, 80.0);
        CGRect buttonFrame = button.frame;
        buttonFrame.size = CGSizeMake(70, 70);
        button.frame = buttonFrame;
        NSLog(@"IPAD");
    }
    else {
        button.frame = CGRectMake(30.0, 30.0, 60.0, 40.0);
        CGRect buttonFrame = button.frame;
        buttonFrame.size = CGSizeMake(45, 45);
        button.frame = buttonFrame;
    }


    [self.view addSubview:button];

我想知道如何在Android应用程序中实现相同的功能? 以下是我当前的布局xml:

<?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:screenOrientation="portrait"
    android:orientation="vertical" >

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

</LinearLayout>

谁能给我任何指示?

编辑:我想将按钮放置在左上角的偏移处。

我总是建议使用相对布局。 在RelativeLayout内部,您可以自由放置View,并且它们也可以重叠。

尝试这样的事情:

<?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:screenOrientation="portrait" >

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

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:alignParentTop="true"
        android:alignParentRight="true" />

</RelativeLayout>

此布局显示ViewPager和一个浮在右上角的按钮。

您可以使用相对布局,这会将ImageButton放在ViewPager顶部,如下所示:

<?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:screenOrientation="portrait"
    android:orientation="vertical" >

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

    <ImageButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:src="@drawable/help" />

</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM