【问题标题】:TextView in alert dialog is not centered or formatted警报对话框中的 TextView 未居中或未格式化
【发布时间】:2017-10-31 22:21:41
【问题描述】:

我想显示一个带有文本消息的弹出对话框,但格式不起作用。
我希望它位于弹出窗口的中心并具有特定字体,但两者均未应用。
我的代码:

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>   

这里有什么问题?

【问题讨论】:

  • textview的重力怎么样?
  • @AmitVaghela:我也尝试了重力和布局重心。不工作
  • 请检查我的回答,如有任何疑问,请询问我
  • 检查我的更新和我的朋友
  • 你得到答案了吗...?

标签: android textview android-alertdialog android-gravity


【解决方案1】:
    <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>   

【讨论】:

    【解决方案2】:
    <?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>
    

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

    【讨论】:

      【解决方案3】:

      创建布局

      <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();
      

      【讨论】:

      • 这也不起作用。此外,您正在使用标题并设置 2 个布局。我不关注这个
      猜你喜欢
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2011-10-26
      • 2014-02-18
      相关资源
      最近更新 更多