How do I change the background of a shape with a corner radius?

问题: I created a intent that gets shown when I click a button, that intent has the theme of a shape which looks like this <shape xmlns:android="http://schemas.android.com/...

问题:

I created a intent that gets shown when I click a button, that intent has the theme of a shape which looks like this

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

    <gradient android:startColor="#4568dc"
              android:endColor="#b06ab3"
              android:angle="45"/>

    <corners android:radius="20dp"/>

</shape>

The only issue though is that with the corner radius it's showing the white background around the corners, how do I get rid of that? I want it to be transparent.

I added this as well, but it didn't change the corners.

<style name="AppTheme.PopMe">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

Added layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".AddItemActivity"
    android:background="@drawable/popbg">

</android.support.constraint.ConstraintLayout>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tester.tester">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".AddItemActivity"
                  android:theme="@style/AppTheme.PopMe"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

FloatingActionBar onClick

    public void ShowPopup(View v){

            myDialog.setContentView(R.layout.activity_add_item);

            categorySpinner = myDialog.findViewById(R.id.spinnerCategory);
            ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.options));
            spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            categorySpinner.setAdapter(spinnerAdapter);



myDialog.show();
    }

and AddItemActivity

public class AddItemActivity extends Activity {

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

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int) (width * .8), (int) (height * .8));

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        params.x = 0;
        params.y = -20;

        getWindow().setAttributes(params);
    }
}

enter image description here


回答1:

Try this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/yourColor"/>
            <corners android:radius="7dp"/>
        </shape>
    </item>
</selector>
  • 发表于 2019-02-26 00:52
  • 阅读 ( 209 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除