java - 如何使我的简单 Activity 成为 android-studio 中的 float Activity ?

标签 java android

这是我的 addbudget 类

public class addbudget extends ActionBarActivity implements View.OnClickListener{


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

                btn66=(Button)findViewById(R.id.btn_addBudget);
                btn66.setOnClickListener(this);
                helper=new DBhelper(addbudget.this);
                txr=(TextView)findViewById(R.id.addbud);

                txtBudget=(EditText)findViewById(R.id.etBudget);

                list = (ListView) findViewById(R.id.list);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                        String h;
                        Cursor row = (Cursor) arg0.getItemAtPosition(position);
                        selected_did = row.getString(0);
                        h = row.getString(1);

                        txtBudget.setText(h);





                    }

                });


                Bundle data_from_list= getIntent().getExtras();
                value_in_tv= data_from_list.getString("passed data key");
                txr.setText(value_in_tv);

                fetchData2();
            }




            private void clearfield(){
                txtBudget.setText("");

            }


            public void onClick(View v) {

                    if (btn66 == v) {
                        checkIfRowPresent(txr.getText().toString());
                    }
                }

            public boolean checkIfRowPresent(String name) {
                SQLiteDatabase db = helper.getWritableDatabase();

                Cursor cursor =
                        db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + name + "'", null, null, null,
                                null, null);
                boolean ret = false;
                if (cursor.getCount() > 0) {

                    Toast.makeText(this, "Budget not add Successfully", Toast.LENGTH_LONG).show();// There is a row present in table1 with the given name
                }
                else{


                    ContentValues value = new ContentValues();
                    value.put(DBhelper.Amount, txtBudget.getText().toString());
                    value.put(DBhelper.Description, txr.getText().toString());


                    db = helper.getWritableDatabase();
                    db.insert(DBhelper.TABLE2, null, value);
                    db.close();
                    clearfield();
                    Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show();
                    fetchData2();
                    Intent i = new Intent(addbudget.this, MainActivity.class);
                    startActivity(i);
                }
                db.close();

                return ret;

            }


            private void fetchData2() {
                db = helper.getReadableDatabase();
                Cursor c = db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + value_in_tv+"'", null, null, null, null);
                adapter = new SimpleCursorAdapter(
                        this,
                        R.layout.addbudget,
                        c,
                        new String[]{DBhelper.Amount},
                        new int[]{R.id.lbl});
                list.setAdapter(adapter);
            }

        }

这是我的 addbudget.xml

 <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="25dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
      >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Category         "
            android:id="@+id/textView3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Budget"
            android:layout_marginTop="20dp"
            android:id="@+id/textView4"
            android:layout_below="@+id/spinner_cat"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_cat"
            android:entries="@array/array_categories"
            android:layout_alignTop="@+id/textView3"
            android:layout_toRightOf="@+id/textView3"
            android:layout_toEndOf="@+id/textView3" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/etBudget"
            android:layout_below="@+id/spinner_cat"
            android:layout_alignLeft="@+id/spinner_cat"
            android:layout_alignStart="@+id/spinner_cat" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Budget"
            android:id="@+id/btn_addBudget"

            android:layout_marginTop="30dp"

            android:layout_below="@+id/etBudget"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignRight="@+id/etBudget"
            android:layout_alignEnd="@+id/etBudget" />
    </RelativeLayout>

在另一个 xml 布局中,我有一个按钮,如果我单击该按钮,则该 Activity 需要作为 float Activity 打开。

我谷歌了这个问题,但答案似乎有点棘手。我不太明白,任何人都可以帮助我将上述 Activity 更改为 float Activity 吗?

最佳答案

这是一个逐步实现的过程。我将复制粘贴我的代码并对其进行解释。

在您的 style.xml 中添加此内容,您可以根据需要进行修改

 <style name="PopupTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>

    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="colorAccent">@color/color_primary</item>
</style>

创建您的Activity 文件及其Layout

Activity的 .class文件中,您可以在onAttachWindow Override方法中处理屏幕尺寸,例如:

 @Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

    switch (screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            this.getWindow().setLayout(900, 755);
            break;
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            this.getWindow().setLayout(1080, 1000); //width x height
            break;
    }
}

最后,在您的 AndroidManifest.xml 文件中添加此内容

<activity android:name=".MyFloatingActivity"
        android:enabled="true"
        android:theme="@style/PopupTheme"<--The theme in style
        android:label="@string/app_name"/>

关于java - 如何使我的简单 Activity 成为 android-studio 中的 float Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198323/

相关文章:

java - Admob 间质性单例模式 java.lang.NullPointerException :

java - Socket异常处理

java - 首选项系统尝试在 "normal"用户在 Linux 上没有写入权限的位置写入锁定文件

android - 我的应用程序屏幕出现在浏览器中

javascript - 在 android 应用程序中实现 javascript 导致问题

Java 项目改变行为

java - SSL 未找到合适的证书

android - 如何为 create-react-native-app 生成 apk 文件(使用 EXPO.IO 组件)

java - 应用程序始终在前台或聚焦或在顶部

java - 无法在我的 firebase 服务中解析方法 'findViewById(int)'