具有圆角而不是100%屏幕宽度的自定义FragmentDialog [英] Custom FragmentDialog with round corners and not 100% screen width

查看:91
本文介绍了具有圆角而不是100%屏幕宽度的自定义FragmentDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义的片段对话框,该对话框具有圆角并且其布局无法填充屏幕宽度(如果只包装其内容,我希望这样做).

I am creating a custom fragment dialog with round corners and with layout that would not fill the screen width (I would prefer if it just wrapped its content).

这是我的rounded_dialog.xml在可绘制文件夹中,我的自定义ThemeWithCorners将其称为对话框的背景.我还尝试将其设置为线性布局的背景,该线性布局创建了其内容,但无济于事.

this is my rounded_dialog.xml in drawable folder, which is called by my Custom ThemeWithCorners as a background for the dialog. I also tried to set it as background to the linear layout which creates its content but nothing works.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" 
>
<solid android:color="@android:color/white"/>
<corners android:radius="20dp"
/>
</shape>

这就是我称呼对话框的方式:

and this is how i call the dialog:

final String FTAG = "TAG_FRAGMENT_DIALOG_CALENDAR";
    dialog = (CalendarDialog) fm.findFragmentByTag(FTAG);
    ft = fm.beginTransaction();
    if (dialog != null)
    {
        ft.remove(dialog);
    }
    dialog = CalendarDialog.newInstance(this);      
    dialog.setCancelable(true);

    ft.add(dialog, FTAG);
    ft.show(dialog);
    ft.commit();

在对话框的onCreate方法中,设置样式和主题:

In onCreate method of the dialog I set the style and theme:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.ThemeWithCorners);      
}

这是onCreateView方法:

This is the onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
getDialog().setCanceledOnTouchOutside(true);
v = (MyCalendar)inflater.inflate(R.layout.calendar_dialog, container, true)
    return v;
}

我还尝试将其添加到onCreateDialog方法中,如关于SO的其他建议所建议的那样,但也不起作用:

I also tried to add this to onCreateDialog method as other answers on SO suggested but did not work either:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    Dialog d =  super.onCreateDialog(savedInstanceState);
    LayoutParams lp=d.getWindow().getAttributes();
    d.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    lp.width=-2;lp.height=-2;lp.gravity=Gravity.CENTER;
    lp.dimAmount=0;            
    lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;

    return d;
}

因此,总而言之,我想要圆角,而不是屏幕的100%宽度,它最好包装其内容.拜托,拜托,我需要一些帮助,我对此感到非常绝望,我很想尝试几天!

So to sum it up, I want round corners, not 100% width of the screen, it preferably should wrap its content. Please, please, I need some help, I am really desperate about this, I´v ebeen trying it for days!

推荐答案

好吧,我刚刚找到了一个解决方案,但是我对此并不满意.

Well, I just found a solution, I am not really happy with it though.

我为对话框设置背景(rounded_dialog.xml),

I set the background (rounded_dialog.xml) for the dialog like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<corners android:radius="10dp" />
<padding android:left="10dp" android:right="10dp"/>
</shape>

然后,我通过下面的"onCreateView"方法将此对话框设置为我的对话框.由于背景是透明的,因此在这段代码中圆角并不是真正必需的,但是填充很重要,因为对话框实际上实际上仍然与屏幕一样宽,但是填充使它看起来好像不是屏幕.

Then I set this to my dialog in its ´onCreateView´ method this way below. The rounded corners are not really necessary in this piece of code as the background is transparent, but the padding is important, because the dialog is still in fact as wide as the screen, but the padding makes it look like it is not.

getDialog().getWindow().setBackgroundDrawableResource(R.drawable.rounded_dialog);

最后,我将对话框组件的背景设置为另一个自定义可绘制对象,该可绘制对象使拐角变圆.我有一个LinearLayout,顶部是RelativeLayout,底部是TextView,所以我将@null设置为父级LinearLayout,并将两个不同的自定义可绘制对象设置为两个部分,其中一个具有圆角的bottomCorners,另一个具有圆形的topCorners.

And in the end I set background of the dialog´s components to another custom drawable which makes the corners round. I have a LinearLayout with RelativeLayout at the top and TextView at the bottom, so I set @null to the parent LinearLayout and set two different custom drawables to the two parts, one of which has rounded bottomCorners and the other one topCorners.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="@drawable/title_round"  
>

<RelativeLayout
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:orientation="horizontal" 
    android:background="@drawable/blue_title_round_top"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    >
<TextView 
    android:id="@+id/calendarHint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_bottom"
    android:layout_gravity="center"
    android:gravity="center"
        />
</LinearLayout>

我相信对此有一个更合适的解决方案,因为这在视觉上是正确的,在功能上不是真正的,但在这种情况下足够正确.

I believe there is a more proper solution to this as this is correct just visually, not really functionally, but enough correct for this case.

这篇关于具有圆角而不是100%屏幕宽度的自定义FragmentDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆