【问题标题】:Floating button over other views - Android其他视图上方的浮动按钮 - Android
【发布时间】:2017-06-18 21:35:18
【问题描述】:

我想制作一个能够显示在EditTexts 上方的按钮或视图 当被点击时,会在特定的焦点EditText 上执行某些操作。我不知道它是如何被调用的,或者是否有任何 3d 方库可以做到这一点。

【问题讨论】:

  • 你可以张贴一张图片或其他东西来详细说明你真正想要的东西吗?

标签: android floating


【解决方案1】:

这就是RelativeLayouts 在这里发挥重要作用的时候,因为您可以将视图放置在另一个视图之上,而顶部的视图具有焦点,反之亦然。所以首先你要把 Android 最新的支持设计库编译到你本地的 build.gradle 文件中来使用 FAB,然后对布局文件做这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="com.davenotdavid.stackoverflow.MainActivity">

    <EditText
        android:id="@+id/et1"
        android:hint="HINT..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et2"
        android:hint="HINT..."
        android:layout_below="@id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et3"
        android:hint="HINT..."
        android:layout_below="@id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>

...然后可能通过在 onCreate() 中显示 Toast 消息(而不是 EditText 的键盘弹出)来测试 FAB 是否将焦点放在焦点 EditText 上,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2016-01-17
    • 2019-08-27
    相关资源
    最近更新 更多