【问题标题】:What is the best way to create smaller size layout for an activity为活动创建较小尺寸布局的最佳方法是什么
【发布时间】:2017-10-29 16:23:32
【问题描述】:

我有一个要求,当用户点击“查看卡片”按钮时,打开一个不覆盖整个屏幕的较小尺寸的活动。

我做了一些研发工作,并找到了实现这一目标的多种选择。

1- 创建一个布局文件,并在您的活动中创建一个自定义对话框,如下所示。

    //OnClickListener 

         Dialog dialog = new Dialog(main.this);
                    dialog.setContentView(R.layout.maindialog);
                    dialog.setTitle("This is my custom dialog box");
                    dialog.setCancelable(true);
   //this can have buttons and other stuff

2- 创建Dialog Fragment

3- 创建一个新活动,并在 Manifest 中添加以下行

android:theme="@android:style/Theme.Translucent"

我无法理解哪种方法更好地实现这一目标。

【问题讨论】:

    标签: android performance android-layout android-fragments


    【解决方案1】:

    您可以在样式中为您的活动创建一个自定义 主题,例如使用自定义导航图标:

    <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="navigationIcon">@drawable/ic_clear_white_24dp</item>
    </style>
    

    然后,您可以将此主题添加到您的活动中,在 Manifest 中:

    <activity
            android:name=".YourActivityName"
            android:theme="@style/DialogTheme" />
    

    此时,您有一个基于 Dialog 的自定义主题的活动。如果你想把 width 和 height 自定义 values 放到你的根布局中,你可以这样做:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="300dp"
        android:layout_height="300dp"/>
    
     <!-- Your activity content... -->
    
     </FrameLayout>
    

    【讨论】:

      【解决方案2】:

      实际上,是否使用活动,对话框取决于您。 例如: 如果数据很短并且只需要显示/写入一些小信息,则使用对话框。 即使您可以尝试隐藏(视图/布局)并单击显示隐藏(视图/布局)与一些动画,工作后隐藏。 即使您现在也可以使用底页 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        • 2012-05-18
        • 2012-03-19
        • 2013-07-22
        • 1970-01-01
        相关资源
        最近更新 更多