我制作了一个透明的自定义进度条。我想以编程方式将进度条移动到屏幕上的某个位置。但是每次自定义进度条都被绘制在屏幕中间。
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>
请指导我,让我知道我哪里出错了。我花了两天时间试图找出问题所在。