繁体   English   中英

如何使我的简单活动成为android-studio中的浮动活动?

[英]How to make my simple activity as floating activity in android-studio?

这是我的预算外课程

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布局中,我有一个按钮,如果单击该按钮,则此活动需要作为浮动活动打开。

我用Google搜索这个问题,但答案似乎有点棘手。我不太了解,有人可以帮助我将上述活动更改为浮动活动吗?

这是分步实施。 我将复制粘贴我的代码并对其进行解释。

在您的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's .class文件中,您可以在onAttachWindow Override methods处理屏幕尺寸,例如:

 @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"/>

暂无
暂无

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

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