2017-01-19 4 views
0

У меня есть макет Android, который используется для отображения диалогового окна предупреждения о «приложении» с кнопкой «Закрыть». Он имеет заголовок в bold с центром, версия приложения и имя разработчика. Все выглядит отлично, за исключением того, что над заголовком есть заметный запас/надпись, и я не могу удалить это пустое пространство.Удалить диалоговое окно Android alert пустое место наверху

Вот раскладка:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView android:orientation="vertical" android:scrollbars="none" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="6.0dp" xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center"> 
     <TextView android:textSize="16.0dip" android:textStyle="bold" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginRight="6.0dip" android:layout_marginBottom="10.0dip" android:text="@string/app_name" /> 
    </LinearLayout> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> 
     <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="@string/version_text" /> 
     <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="@string/app_version" /> 
    </LinearLayout> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> 
     <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:text="@string/about_developer" /> 
    </LinearLayout> 

    <LinearLayout android:gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" xmlns:android="http://schemas.android.com/apk/res/android"> 
     <Button android:id="@id/close_btn" android:layout_width="110.0dip" android:layout_height="44.0dip" android:layout_marginTop="4.0dip" android:layout_marginBottom="4.0dip" android:text="@string/about_close" /> 
    </LinearLayout> 

    </LinearLayout> 
</ScrollView> 

И вот что он показывает:

about dialog window

Я попытался добавить android:layout_marginTop="-10.0dip" атрибут во многих тегов и поиск SO для подобного вопроса, но я могу» t найти его. Я был бы благодарен, если кто-нибудь покажет мне, как решить эту проблему.

ответ

3

Вы можете попробовать сделать свой собственный диалог с настраиваемой View с помощью DialogFragment и установить этот

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

Или, может быть, просто попробуйте использовать простой Dialog, не AlertDialog как это

Dialog dialog = new Dialog(theContext); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(yourCustomView); 
dialog.show(); 
+0

Я был на полпути с тем же ответом. Отлично сработано. –

+0

Я пытаюсь применить ваш ответ, и Android Studio сообщает мне, что «MainActivity не является закрывающим классом» каких-либо идей? Пожалуйста помоги. –

+0

отредактирован! положите контекст! – DevAndroid

0

Вместо этого используйте RelativeLayout (приведенный ниже код необходимо скорректировать в соответствии с вашими потребностями, но он дает общую схему):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView 
    android:text="App Name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/textView" /> 

<TextView 
    android:text="Version 1.0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="29dp" 
    android:id="@+id/textView2" /> 

<TextView 
    android:text="Developed by Name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="23dp" 
    android:id="@+id/textView3" /> 

<Button 
    android:text="Close" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView3" 
    android:layout_alignLeft="@+id/textView2" 
    android:layout_alignStart="@+id/textView2" 
    android:layout_marginTop="42dp" 
    android:id="@+id/button2" /> 
</RelativeLayout> 

Вы также можете использовать Диалог, как и другие.

EDIT:

Вы можете попробовать использовать пользовательские Dialog вместо затем с помощью DialogFragment. Для того, чтобы вызвать диалоговое, используйте: new CustomDialogFragment().show(getSupportFragmentManager(), "mydialog");

Затем создайте новый класс, расширяющий DialogFragment, и использовать что-то похожее на это:

public class DonateDialogFragment extends DialogFragment implements View.OnClickListener 
{ 

private AlertDialog dialog; 

public interface CustomDialogListener { 
    void buttonClicked(); 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    if (!(activity instanceof CustomDialogListener)) { 
     throw new ClassCastException(activity.toString() + " must implement CustomDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder customDialogMain = new AlertDialog.Builder(getActivity()); 
    donateDialogMain.setCancelable(true); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    View view = inflater.inflate(R.layout.--YOUR-CUSTOM-DIALOG-LAYOUT-HERE--, null); 

    view.findViewById(R.id.close_btn).setOnClickListener(this); 

    customDialogMain.setView(view); 

    dialog = customDialogMain.show(); 
    return dialog; 
} 

public void onClick(View v) { 
    // does something very interesting 
    switch (v.getId()) 
    { 
     case R.id.close_btn: 
      ((CustomDialogListener) getActivity()).buttonClicked(); 
      dialog.dismiss(); 
      break; 
    } 
} 

}

+0

Я скорректировал XML и попробовал его, проблема все еще сохраняется. –

+0

Я добавил шаги, чтобы использовать диалог вместо этого, он должен вас поймать. :) –

0

вы можете использовать этот стиль прямо на style.xml

<style name="Theme.UserDialog.AppCompat" parent="Theme.AppCompat.Dialog"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowCloseOnTouchOutside">true</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:windowBackground">@android:color/white</item> 
    <item name="android:minWidth">600dp</item> 
</style> 

Примените этот стиль к классу активности, который вы хотите установить в качестве предупреждения диалога

android:theme="@style/Theme.UserDialog.AppCompat" 

и начать деятельность с помощью Intent как

Intent activity= new Intent(this, AlertActivity.class); 
startActivity(activity); 
Смежные вопросы