我试图通过扩展RelativeLayout
来创建一个复合视图。它由三个小部件组成。一个ImageView
,一个ProgressBar
和一个TextView
。但由于某种原因,我无法看到ImageView
和TextView
。附上以下代码。有什么建议吗?
public class ProgressView extends RelativeLayout {
@InjectView(R.id.ivItemPic)
ImageView ivItemPic;
@InjectView(R.id.tvMessage)
TextView tvMessage;
@InjectView(R.id.pbLoading)
ProgressBar pbLoading;
int defColor = 0xff669900;
public ProgressView(Context context) {
super(context);
init(context,null, 0);
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs, 0);
}
public ProgressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs, defStyle);
}
public void init(Context context, AttributeSet attrs, int defStyleAttr) {
inflate(context, R.layout.component_progress_view, this);
ButterKnife.inject(this);
TypedArray aTypedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView, defStyleAttr, 0);
final int textSize = aTypedArray.getDimensionPixelSize(
R.styleable.ProgressView_pvTextSize,
dipsToPix(R.dimen.font_medium));
final int textColor = aTypedArray.getColor(R.styleable.ProgressView_pvTextColor,defColor);
final Drawable imgSrc = aTypedArray.getDrawable(R.styleable.ProgressView_pvImageSrc);
ivItemPic.setImageDrawable(imgSrc);
tvMessage.setTextSize(textSize);
tvMessage.setTextColor(textColor);
aTypedArray.recycle();
}
/**
* Helper method to convert dips to pixels.
*/
private int dipsToPix(float dps) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps,
getResources().getDisplayMetrics());
}
public void setText(String message) {
tvMessage.setText(message);
}
public void imageVisibility(boolean visible) {
ivItemPic.setVisibility(visible ? VISIBLE : GONE);
}
public void progressIndeterminate(boolean indeterminate) {
pbLoading.setIndeterminate(indeterminate);
}
public void setItemImage(Drawable drawable) {
ivItemPic.setImageDrawable(drawable);
}
public void setItemImageRes(int res) {
ivItemPic.setImageResource(res);
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/ivItemPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pbLoading"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:src="@drawable/ic_action_info"/>
<ProgressBar
android:id="@+id/pbLoading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="true"
android:visibility="visible" />
<TextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pbLoading"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/horizontal_margin"
android:layout_marginLeft="@dimen/horizontal_margin"
android:textSize="@dimen/font_medium"
android:textColor="@color/grey_normal"
android:gravity="center"
android:text="Message"/>
布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:errorview="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context="com.mobility.ui.SyncFragment">
<com.mobility.customviews.SuccessView
android:id="@+id/svSuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="center"
app:svText="Success"
app:svTextColor="@android:color/holo_green_dark"/>
<tr.xip.errorview.ErrorView
android:id="@+id/evError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
errorview:ev_errorSubtitle="@string/err_conn"
errorview:ev_errorTitle="@string/oops"
errorview:ev_showRetryButton="true"
errorview:ev_showSubtitle="true"
errorview:ev_showTitle="true"
android:gravity="center"
android:visibility="gone"
android:paddingLeft="@dimen/horizontal_margin"
android:paddingRight="@dimen/horizontal_margin"
errorview:ev_retryButtonTextColor="@color/error_retry"/>
<com.mobility.customviews.ProgressView
android:id="@+id/progressView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:pvText="ZzZZ"/>