2016-10-22 57 views
0

我在編程時遇到了問題。我認爲我已經通過爲對話框的主體指定了一個固定的百分比高度來解決它。我認爲這是不好的形式,因爲用戶可能有一個纖細的顯示屏,或設置大的字體,這將削減EditText框。Android對話框佈局破解如何修復?

無論如何,該解決方案已超越模擬器失敗。

它看起來像Android是分配一個固定的高度來一個對話框,如果我的自定義標題吞噬了太多這樣的高度,擠一切了。這是正確的,我該如何解決它? 問題 enter image description here

public class GetUserNameDialogFragment extends DialogFragment { 

    final String TAG = "GetUserNameDialog"; 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle); 
     //TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     final View sunburst =inflater.inflate(R.layout.dialog_user_name, null); 
     builder.setView(sunburst); 


     builder.setCancelable(false) 
       .setPositiveButton("Let's go!", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         Log.wtf(TAG, "button press"); 
         EditText name = (EditText) sunburst.findViewById(R.id.nameEditText); 
         String userName = name.getText().toString().trim(); 
         //TODO needs to be validated 
         SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE); 
         SharedPreferences.Editor editor = sharedPref.edit(); 
         editor.putString("userName", userName); 
         editor.commit(); 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

這裏的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:adjustViewBounds="true" 
     app:srcCompat="@drawable/ic_ball_sunburst_classic" 
     android:background="@color/colorAccent" 
     /> 
    <LinearLayout 
     android:layout_width="250dp" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:layout_height="125dp"> 
    <EditText 
     android:id="@+id/nameEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="8dp" 
     android:hint="Enter your first name"/> 
    </LinearLayout> 
</LinearLayout> 

所有幫助非常感謝,我的應用程序的第一印象 - 什麼是災難!

回答

0

你爲什麼不使用Dialog代替AlertDialog

我建立使用對話框的例子,它完美的作品

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_gravity="center_horizontal" 
android:layout_width=" 290dp" 
android:layout_height="wrap_content" 
android:background="#4CAF50"> 


<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scaleType="fitCenter" 
    android:adjustViewBounds="true" 
    android:src="@drawable/base" 
    android:background="@color/colorAccent" 
    /> 

    <EditText 
     android:textColor="#fff" 
     android:textColorHint="#fff" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:id="@+id/nameEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Enter your first name"/> 

<Button 
    android:layout_marginRight="10dp" 
    android:layout_marginEnd="10dp" 
    android:id="@+id/letsGo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:text="Let's Go" 
    android:textColor="#fff" 
    android:background="@android:color/transparent"/> 

final Dialog dialog = new Dialog(context); 
    if(dialog.getWindow() != null){ 
     dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    } 
    dialog.setContentView(R.layout.dialog); 

    Button letsGO = (Button) dialog.findViewById(R.id.letsGo); 
    EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText); 

    letsGO.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show(); 
      dialog.dismiss(); 
     } 
    }); 
    dialog.show();