【问题标题】:How to add a layout file to only take half the page of Android activity如何添加布局文件以仅占用 Android 活动页面的一半
【发布时间】:2016-03-15 12:56:12
【问题描述】:

基本上,我希望出现一个弹出框,以便用户可以填写表单,而不是转到下一页。

我不知道该怎么做,但我已经附上了我想要的图片

我就是这样将我的 xml 文件放入该活动中的方式

谢谢

【问题讨论】:

  • 不要使用弹出对话框,而是使用活动
  • 使用AlertDialog.Builder 更容易,您可以根据需要设置样式(通常的对话框也是 ofc)
  • 我尽量远离AlertDialog.Builder,有没有其他方法可以做到这一点?
  • 您应该使用DialogFragment,并覆盖方法onCreateView,您可以在其中传递您想要的布局。
  • 我该怎么做?抱歉,我是 android 开发新手

标签: android android-layout android-activity android-xml


【解决方案1】:

使用 Fragment 是最好的方法。 http://developer.android.com/reference/android/support/v4/app/DialogFragment.html

您也可以使用 Framelayout。 进行一些自定义很容易。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zhangjunyi.testfornet.MainActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        //your activity layout 
    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        //your dialog view, may do some custome
        //may use some animation to show, when the animation end >  >             //set the view visible, use a transparent background for
         // the view make
        //it looks like a dialog.


    </FrameLayout>
</FrameLayout>

【讨论】:

    【解决方案2】:

    您可以使用对话框视图在同一个活动中添加 UI 并在其上执行任务。在这里,您必须根据需要设置 UI 布局。

    public void showDialog( Context context ) {
    
        Dialog mDialog;
    
        mDialog = new Dialog(context);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.layout_dialog);
        mDialog.setCancelable(false);
        mDialog.show();
    
        RatingBar ratingBar = (RatingBar) mDialog.findViewById(R.id.dlg_rating_bar_);
        TextView tvMessage = (TextView) mDialog.findViewById(R.id.dlg_tv_message);
        EditText etComment = (EditText) mDialog.findViewById(R.id.dlg_et_comment);
        Button btnAdd = (Button) mDialog.findViewById(R.id.dlg_btn_add);
        Button btnDiscard = (Button) mDialog.findViewById(R.id.dlg_btn_discard);
    
        // do your stuff here
    
    }
    

    【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多