警报对话框中的TextView未居中或格式化

时间:2017-05-31 08:14:14

标签: android textview android-alertdialog android-gravity

我想显示带有短信的弹出式对话框,但格式不起作用 我希望它在弹出窗口的中心并使用特定的字体,但都没有应用 我的代码:

AlertDialog dialog = new AlertDialog.Builder(context).create();  
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
View view = LayoutInflater.from(mContext).inflate(R.layout.my_view, null);  
dialog.setView(view);  
TextView messageView = (TextView) view.findViewById(R.id.message);  
messageView.setTextIsSelectable(true);  
messageView.setText("This is the message to show");  
dialog.show(); 

布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_gravity="center_horizontal"  
    android:background="@color/red"  
    android:layout_width="match_parent"    
    android:layout_height="match_parent">  
    <TextView    
        android:id="@+id/message"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:fontFamily="sans-serif"  
        android:textSize="14sp"/>  
</LinearLayout>   

这是什么问题?

enter image description here

3 个答案:

答案 0 :(得分:0)

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_horizontal"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/message"
        android:text="asasasasas"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fontFamily="sans-serif"
        android:textSize="14sp"/>
</LinearLayout>   

答案 1 :(得分:0)

创建布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/red"
android:orientation="vertical">

<TextView
    android:id="@+id/message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"        
    android:textSize="14sp" />
</LinearLayout>

创建对话框

AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom, null);
            dialog.setView(view1);
            TextView messageView = (TextView) view1.findViewById(R.id.message);
            messageView.setTextIsSelectable(true);
            messageView.setText("the is text demo my friend");
            dialog.show();

enter image description here

答案 2 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center_horizontal"
    android:background="@color/red"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="This is the message to show"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textSize="14sp" />
</RelativeLayout>

试试这个。希望这可以解决您的问题。