-3

我想用文本消息显示弹出对话框,但格式不起作用。
我希望它位于弹出窗口的中心并使用特定的字体,但都不适用。
我的代码:警报对话框中的TextView未居中或格式化

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

+0

textview的引力怎么样? –

+0

@AmitVaghela:我也尝试过重力和布局重心。不起作用 – Jim

+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> 
+0

不起作用.. – Jim

+0

尝试更新代码.... –

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

+0

这也不起作用。此外,您正在使用标题并设置2个布局。我不遵循这个 – Jim

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> 

试试这个。希望这会解决你的问题。