首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有新活动的情况下,是否可以在主细节集合中显示DialogFragment?

问在没有新活动的情况下,是否可以在主细节集合中显示DialogFragment?
EN

Stack Overflow用户
提问于 2014-07-27 00:50:43
回答 5查看 1.1K关注 0票数 2

我正在寻找一种在不创建新活动的情况下在单窗格模式下显示DialogFragment的方法。

我最初在我的Android应用程序中设置了一个DialogFragment作为弹出对话框,目的是最终实现大型设备的主细节模式。

然而,现在我希望最终实现主细节设置,我遇到了各种各样的UI复杂问题。基本上,我希望有一个类似于“上下文操作模式”的更新操作栏。这需要在双窗格模式下进行一些规划,但对于弹出对话框却根本不起作用(除非我错过了显示操作栏和弹出对话框的方法)。

我不想在非平板/大型设备上创建一个新的活动来容纳详细的DialogFragment,因为现有的活动中有很多与DB相关的代码。但是,我很难只执行FragmentTransaction.replace,因为主视图是基于兼容性库v4演示的修改后的v4。我没有可以替换的片段,除非我将整个寻呼机封装在一个片段中,而且我担心嵌套片段是一种黑客攻击,从长远来看,它会使事情变得复杂,而更简单。我说错了吗?

我也在使用ActionBarCompat,这会使事情复杂化,因为有一些UI选项没有移植。如果这意味着找到一个干净的解决方案,我会考虑使用API 11+。

顺便说一句,我开始研究Commonsware的主细节库,但这是一些需要摸索和吸收的代码,我认为它需要一些可能的大更改才能使我的代码兼容。

有什么建议或意见吗?我觉得我离这个太近了,看不出怎么简化它.

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-08-12 11:52:13

试试这个:

这是您的活动布局的XML。您需要做的是将所有活动内容封装在一个布局中(就像我在id =all_activity_content_id中所做的那样)。

现在,在活动中再放两个视图:-一个用于您的对话框的RelativeLayout和一个用于使背景透明的视图。

注意:确保活动的根布局是RelativeLayout

代码语言:javascript
复制
<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"
tools:context="in.curium.testandroid.MainActivity" >

<!-- all your activity's existing code goes here -->

<RelativeLayout
    android:id="@+id/all_activity_content_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/holo_red_light" >

    <ToggleButton
        android:id="@+id/toggle_dialog_box_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:textOff="@string/show_dialog"
        android:textOn="@string/hide_dialog" />
</RelativeLayout>

<!-- translucent black background behind the dialog -->

<View
    android:id="@+id/black_layer_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:alpha="0.6"
    android:background="@android:color/black"
    android:visibility="gone" />

<!-- your dialog layout -->

<RelativeLayout
    android:id="@+id/dialog_id"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:background="#00BFFF"
    android:visibility="gone" >

    <TextView
        android:id="@+id/dialog_title_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:text="@string/dialog_title"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_title_id"
        android:text="@string/dialog_description" />
</RelativeLayout>

​

在activity中添加两个方法来显示和隐藏对话框布局。重写“后退”按钮以在可见时关闭对话框,否则调用“超级”。

代码语言:javascript
复制
public class MainActivity extends Activity {

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


    ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle_dialog_box_id);
    toggleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View toggleB) {
            boolean isOn = ((ToggleButton) toggleB).isChecked();
            if (isOn) {
                showDialog();
            } else {
                hideDialog();
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void showDialog() {
    View opaqueBackground = findViewById(R.id.black_layer_id);
    RelativeLayout dialog = (RelativeLayout) findViewById(R.id.dialog_id);
    opaqueBackground.setVisibility(View.VISIBLE);
    dialog.setVisibility(View.VISIBLE);
}

private void hideDialog() {
    View opaqueBackground = findViewById(R.id.black_layer_id);
    RelativeLayout dialog = (RelativeLayout) findViewById(R.id.dialog_id);
    opaqueBackground.setVisibility(View.GONE);
    dialog.setVisibility(View.GONE);
}

@Override
public void onBackPressed() {
    ToggleButton toggleB = (ToggleButton) findViewById(R.id.toggle_dialog_box_id);
    boolean isOn = toggleB.isChecked();
    if (isOn) {
        toggleB.setChecked(false);
        hideDialog();
    } else {
        super.onBackPressed();
    }
}}

现在,您想要放在对话框中的任何代码都可以访问活动中的数据库适配器。

将对话框定位在细节片段的中心将是有点棘手的。

希望这能有所帮助。

源代码:https://github.com/testv200/DialogInsideAcitvity

票数 1
EN

Stack Overflow用户

发布于 2014-07-27 01:35:47

(1)您绝对不应该创建一个完全新的活动,其唯一目的是容纳一个新的DialogFragment。如果你要创建一个新的活动,你可以给它一个对话主题,并显示作为你的对话.这将消除完全显示DialogFragment的需要。

(2)你的问题有点笼统,我无法给出一个自信的答案.您提到了“上下文操作模式”、“对话片段”、“片段选项卡寻呼机”、“嵌套片段”等,我不知道它们是如何结合在一起的,或者具体地说您想要实现什么。我所知道的是,无论配置如何,主机活动都应该是负责执行片段事务(例如显示DialogFragment)的活动,因为这将大大降低代码复杂性(特别是当屏幕上显示的片段数量因屏幕大小而异时)。您的片段是否通过活动回调方法(如这里和这里所描述的)与活动通信?在您的代码中,您在哪里显示DialogFragment:在您的活动中还是在一个主/详细片段中?

票数 3
EN

Stack Overflow用户

发布于 2014-08-12 16:51:05

这是不可能的,因为您正在使用活动

如果您扩展FragmentActivity而不是只因为您想显示DialogFragment而不是它所需的FragmentManager引用,那么在正常的活动中是不可能的。它只在FragmentActivity内部或API级别13以上提供

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24976875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档