【问题标题】:Positioning Custom ProgressDialog Android定位自定义 ProgressDialog Android
【发布时间】:2013-11-25 08:14:42
【问题描述】:

我已经制作了一个透明的自定义进度条。我想以编程方式将进度条移动到屏幕上的某个位置。但是每次自定义进度条都被绘制在屏幕中间。

public class CustomProgressBar extends Dialog {
ProgressBar mProgressBar;
TextView mTextMessage;

public CustomProgressBar(Context context, int id) {
    super(context, R.style.ProgressDialogTheme);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_progree_dialog,
            null);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);//the progress bar does not get aligned to left top with the defined margin space.
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);//
    params.setMargins(50, 50, 0, 0);//

    // layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id);

    this.addContentView(view, params);

}

}

这就是我在我的活动中使用它的方式

  Dialog mProgressBar = new CustomProgressBar(
            MyActivity.this,
            otherView.getId());

我的进度条布局文件custom_progree_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ProgressBar
    android:id="@+id/id_server_connection_progress_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/somedrwablefile" />

<TextView
    android:id="@+id/id_server_connection_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="@dimen/padding_30px"
    android:layout_marginTop="@dimen/padding_4px"
    android:layout_toRightOf="@id/progress_dialog"
    android:text="@string/connectingMessage"
    android:textColor="@color/white"
    android:textSize="@dimen/padding_40px" />

</RelativeLayout>

请指导我,让我知道我哪里出错了。我花了两天时间试图找出问题所在。

【问题讨论】:

  • LayoutParam 的类型与周围的布局类型有关。什么布局类型,自定义 ProgressBar 包含在里面?如果是LinearLayout,你必须使用LinearLayoutParams来定位你的Dialog。
  • 要添加进度条的 Activity 的布局类型是 relativelayout。
  • 接下来要检查:你能发布周围布局的XML吗? ProgressBar 应该显示在左上角。它在哪里得到实际位置。

标签: java android android-layout android-view android-progressbar


【解决方案1】:

所以我通过更改我的 custom_progress_dialog 并将进度条和文本对齐到我想要的位置解决了这个问题。另外我不再使用自定义类。我只是在运行时膨胀布局并添加它作为我活动中的子视图。

   if (mView == null) 
   {
    mView = getLayoutInflater().inflate(R.layout.server_connection_progree_dialog,null);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    MyActivity.this.addContentView(mView, layoutParams);
   }
   else 
     if (!mView.isShown()) 
      {
         mView.setVisibility(View.VISIBLE);
      }

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2017-03-06
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多