上代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.setGravity(Gravity.LEFT | Gravity.TOP);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.width = 1;
layoutParams.height = 1;
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
window.setAttributes(layoutParams);
Log.e(TAG, "onCreate: 显示");
}
}
主题设置为透明
<style name="TransparentStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoDisplay">false</item>
<item name="android:backgroundDimEnabled">false</item><!--activity不变暗-->
</style>
AndroidManifest.xml 中
<activity android:name=".MainActivity"
android:theme="@style/TransparentStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
本文介绍如何在Android应用中实现透明主题,并通过代码设置Activity的精确位置,使其能够固定在屏幕左上角,宽度和高度仅占1像素。
9788

被折叠的 条评论
为什么被折叠?



