可以參考下面的範例 :
Layout XML :
XML 內容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/buttonTextDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog 預設文字位置"
/>
<Button
android:id="@+id/buttonTextCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog 文字置中對齊"
/>
</LinearLayout>
Main 主程式 :
程式碼
public class TomKuoTest extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.main_test );
setTitle("Android Dialog Message Center");
findViewById(R.id.buttonTextDefault).setOnClickListener(this);
findViewById(R.id.buttonTextCenter).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if( v == findViewById(R.id.buttonTextDefault) )
{
new AlertDialog.Builder(this)
.setMessage( "Dialog 預設文字位置" )
.show();
}
else if( v == findViewById(R.id.buttonTextCenter) )
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage( "Dialog 文字置中對齊" );
AlertDialog ad = builder.create();
ad.show();
// 取得 AlertDialog Message, 然後設定對齊位置為水平置中
TextView messageText = (TextView)ad.findViewById( android.R.id.message );
messageText.setGravity( Gravity.CENTER_HORIZONTAL );
}
}
}
範例結果 :


