自定義 Dialog 中的 ProgressBar 在索尼手機上出現拉伸、偏移,其他品牌正常。
ProgressBar 的 indeterminateDrawable 渲染有特殊處理,當父佈局背景非透明或尺寸為 wrap_content 時觸發繪製異常。rotate + shape=ring 動畫,避免系統默認樣式差異。wrap_content。android:gravity="center" 而非 center_horizontal。<!-- res/layout/common_dialog.xml -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#66000000"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="56dp"
android:layout_height="56dp"
android:paddingTop="10dp"
android:indeterminateDrawable="@drawable/progress_bar_drawable"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="請稍等"
android:textColor="@android:color/white"
android:textSize="15sp" />
</LinearLayout>
<!-- res/drawable/progress_bar_drawable.xml -->
<animated-rotate xmlns:android="https://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0" android:toDegrees="720">
<shape android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="15"
android:useLevel="false">
<gradient
android:type="sweep"
android:startColor="#999999"
android:endColor="#FFFFFF"
android:centerY="0.5"
android:useLevel="false" />
</shape>
</animated-rotate>
private void showProgress() {
mProgressDialog = new Dialog(this, R.style.CustomDialog);
mProgressDialog.setContentView(R.layout.common_dialog);
mProgressDialog.setCanceledOnTouchOutside(true);
// 不要手動設置 Window 布局參數,讓 wrap_content 自然測量
mProgressDialog.show();
}