【问题标题】:How to put borders to the entire screen in the Android application?如何在Android应用程序中为整个屏幕设置边框?
【发布时间】:2018-08-09 12:43:15
【问题描述】:

我希望能够在我的应用程序的整个屏幕中添加边框,如下所示:

这怎么可能实现?

【问题讨论】:

    标签: android android-layout fullscreen


    【解决方案1】:

    使用示例更新(API 21 或更高版本):

    第 1 步:

    在你的drawable资源中创建一个边框xml,如下所示,

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item><layer-list>
            <item>
                <shape>
                    <stroke android:width="4dp" android:color="#FF3B3B"/>
                    <solid android:color="@android:color/transparent"/>
                </shape>
            </item>
        </layer-list>
        </item>
    
    </selector>
    

    第 2 步:

    修改styles.xml中的activity主题设置状态栏颜色为透明如下,

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:statusBarColor">@android:color/transparent</item>
        </style>
    

    第 3 步:

    为您的活动 xml 使用自定义工具栏,如下所示,

    <?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"
        android:background="@drawable/border"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_marginRight="4dp"
            android:layout_marginStart="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginTop="4dp"
            app:layout_constraintTop_toTopOf="parent"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:orientation="vertical">
                <ImageView
                    android:background="@color/colorPrimaryDark"
                    android:layout_width="match_parent"
                    android:layout_height="24dp" />
                <TextView
                    android:text="@string/app_name"
                    android:textStyle="bold"
                    android:textSize="16sp"
                    android:textColor="#FFF"
                    android:gravity="center_vertical"
                    android:layout_width="match_parent"
                    android:layout_marginStart="16dp"
                    android:layout_height="?attr/actionBarSize" />
    
            </LinearLayout>
        </android.support.v7.widget.Toolbar>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    第 4 步:

    最后,在你的活动中 onCreateMethod,

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
        }
    

    这是一个示例输出:

    【讨论】:

    • 这不是我需要的。正如您在示例中看到的那样,我试图在整个屏幕中放置一个边框,而不仅仅是在内部布局中,即使使用我的自定义工具栏也不起作用。这是你的结果link
    • @DenebChorny 如果答案与您的问题不匹配,为什么您将其标记为已接受?这样别人会很困惑
    • @MohammadAli,答案是在 Deneb 发表评论后更新的。请阅读答案。谢谢
    • @MRah 好的,谢谢,因为我认为没有修复的 cmets 中没有进一步的更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多