首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android ProgressBar 索尼手機扭曲變形解決方案

     編輯:程式設計疑難解答

問題現象

自定義 Dialog 中的 ProgressBar 在索尼手機上出現拉伸、偏移,其他品牌正常。

根因分析

  • 索尼某些 ROM 對 ProgressBar 的 indeterminateDrawable 渲染有特殊處理,當父佈局背景非透明或尺寸為 wrap_content 時觸發繪製異常。
  • Dialog 窗口寬度固定 500px 導致在不同密度設備上佈局擠壓。

解決方案

  1. 顯式指定 indeterminateDrawable,使用自定義 rotate + shape=ring 動畫,避免系統默認樣式差異。
  2. 固定 ProgressBar 寬高(如 56dp),不使用 wrap_content。
  3. 父佈局居中: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>

Dialog 調用優化

private void showProgress() {
    mProgressDialog = new Dialog(this, R.style.CustomDialog);
    mProgressDialog.setContentView(R.layout.common_dialog);
    mProgressDialog.setCanceledOnTouchOutside(true);
    // 不要手動設置 Window 布局參數,讓 wrap_content 自然測量
    mProgressDialog.show();
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved