2016-03-09 3 views
1

Мне нужно использовать свою активность AppCompat как диалоговое окно. Для этого я попробовал такое решение, которое отвечало в StackOverflow. Но ничего не получилось. Пожалуйста, ответьте мне. Я получаю активность как диалог. Но он показывает очень узкий как в высоте & ширины.Как сделать работу AppCompat как диалоговое окно?

Я использовал следующую тему:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog"> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:background">@android:color/transparent</item> 
</style> 

enter image description here

+0

Ваш вопрос каким-то образом направлен на окно самого диалога? В MATCH_PARENT и? Что-то похожее на это - http://stackoverflow.com/questions/11613825/how-to-create-dialog-which-will-be-full-in-horizontal-dimension -? –

+0

Вы также можете попробовать добавить *** true ***, http://stackoverflow.com/questions/9859220/full-screen-in-customize-theme –

+0

Извините. Я не получил его. Я загрузил скриншот того, что получаю. –

ответ

0

Вы можете использовать DialogFragment и настроить макет соответствующим образом.

public class CustomDialogFrag extends DialogFragment{ 
static FragmentManager fragmentManager; 
    public static CustomDialogFrag showDialog(FragmentManager fm){ 
     CustomDialogFrag customDialogFrag=new CustomDialogFrag(); 
     fragmentManager=fm; 
     return customDialogFrag; 
    } 
@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 
     View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null); 
     alertDialogBuilder.setView(view); 
     setupUI(view); 
     alertDialogBuilder.setTitle("Notification Message"); 
     alertDialogBuilder.setIcon(R.drawable.notificationicon); 
     alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
     return alertDialogBuilder.create(); 
    } 
void setupUI(View view){ 
     TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias); 
     TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime); 
     TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage); 
     textViewOne.setText("Text 1"); 
     textViewTwo.setText("Text 2"); 
     textViewThree.setText("Text 3"); 
    } 
} 


И dialogfrag_layout.xml будет

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/margin_10" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtEventAlias" 
     android:text="Sample" 
     android:textColor="@android:color/darker_gray" 
     android:textSize="@dimen/textSizeMedium" 
     android:padding="@dimen/margin_10" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtTime" 
     android:text="Sample" 
     android:textColor="@android:color/darker_gray" 
     android:textSize="@dimen/textSizeMedium" 
     android:padding="@dimen/margin_10" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtLogMessage" 
     android:text="Sample" 
     android:textColor="@android:color/darker_gray" 
     android:textSize="@dimen/textSizeMedium" 
     android:padding="@dimen/margin_10" 
     /> 
</LinearLayout> 

Для вызова этого диалогового окна из Fragment:

DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager()); 
dialogFragment.show(getActivity().getFragmentManager(), "tag"); 
0

В OnCreate вашей деятельности поместим следующие строки:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_your); 

    // Make the window's width full sized 
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); 
    Window window = getWindow(); 

    layoutParams.copyFrom(window.getAttributes()); 
    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; 
    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; 

    window.setAttributes(layoutParams); 
} 

Протестировано и работает. Вы можете установить как ширину, так и высоту до WRAP_CONTENT, если необходимо.

Смежные вопросы