自定义视图不显示
以下是我希望显示自定义视图的布局的XML。自定义视图不显示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget273"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
>
<TextView
android:id="@+id/csp_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Source"
android:textSize="40sp"
android:textColor="#ffc83200"
android:gravity="center"
android:paddingTop="15dip"
android:paddingBottom="75dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.bookcessed.booksearch.SearchProviderButton
android:id="@+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/csp_tv_title"
rs:providerName="BOOKP" />
</RelativeLayout>
>
这里是自定义视图的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/pv_rl_strings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/pv_tv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_toRightOf="@+id/pv_tv_and"
android:textSize="18sp"
android:textColor="#ff11ab37"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/pv_tv_and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and "
android:textSize="18sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#ff000000"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/pv_tv_browse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browse"
android:textSize="18sp"
android:textColor="#ff0077bb"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/pv_tv_and" />
<ImageView
android:id="@+id/pv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pv_tv_search"
android:layout_centerInParent="true"
android:layout_alignTop="@+id/pv_tv_and"
android:src="@drawable/bookp_logo"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/pv_rl_genres"
android:layout_width="wrap_content"
android:layout_below="@+id/pv_rl_strings"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/pv_genre1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre2"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre1"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre3"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre2"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre4"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre3"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre5"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre4"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre6"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre5"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre7"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre6"
>
</ImageView>
<ImageView
android:id="@+id/pv_genre8"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre7" />
<ImageView
android:id="@+id/pv_genre9"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/pv_genre8" />
</RelativeLayout>
</RelativeLayout>
这里是类SearchProviderButton:
public class SearchProviderButton extends LinearLayout {
private Connector connector;
private Context context;
private View inflatedView;
public SearchProviderButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.SearchProviderButton);
connector = SearchProvider.valueOf(a.getString(R.styleable.SearchProviderButton_providerName)).getConnector();
this.context = context;
setFocusable(true);
setBackgroundColor(Color.WHITE);
setVisibility(VISIBLE);
//setOnClickListener(listenerAdapter);
setClickable(true);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater li = LayoutInflater.from(context);
inflatedView = li.inflate(R.layout.provider_view, null);
ImageView logo = (ImageView)inflatedView.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)inflatedView.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)inflatedView.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
public Connector getConnector(){
return connector;
}
public void setConnector(Connector connector){
this.connector = connector;
}
private void setgenreIcons(){
int[] genreIconDrawables = {R.id.pv_genre1,R.id.pv_genre2, R.id.pv_genre3,
R.id.pv_genre4, R.id.pv_genre5, R.id.pv_genre6, R.id.pv_genre7,
R.id.pv_genre8, R.id.pv_genre9};
ArrayList<Genre> availgenre = connector.getAvailablegenres();
availgenre.remove(Genre.ALL);
int counter = 0;
for(int genreIVid : genreIconDrawables){
ImageView curgenreImageView = (ImageView)inflatedView.findViewById(genreIVid);
if(counter < availgenre.size() - 1){
curgenreImageView.setImageResource(availgenre.get(counter).getDrawable());
} else {
curgenreImageView.setVisibility(GONE);
}
counter++;
}
}
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
if (gainFocus == true){
this.setBackgroundColor(Color.rgb(255, 165, 0));
} else {
this.setBackgroundColor(Color.WHITE);
}
}
}
下面是类加载包含我的自定义组件的XML代码:
bookPServiceProviderButton = (SearchProviderButton)findViewById(R.id.csp_spb_1);
bookPServiceProviderButton.setOnClickListener(SPBOnClickListener);
bookPServiceProviderButton.setConnector(new bookPConnector());
bookPServiceProviderButton.setVisibility(View.VISIBLE);
编辑:第一评论后,我加入这个代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
现在,它的宽度和高度,但没有显出它里面了!
您的自定义布局视图中没有出现,因为你没有把任何东西进去。在您的onFinishInflate
中,您有行inflatedView = li.inflate(R.layout.provider_view, null);
但您不会将其添加到您的视图中。您有两个选项可将视图添加到自定义布局视图。
更改定制视图延长RelativeLayout
,改变包围RelativeLayout
到<merge>
在provider_view.xml,并修复findViewId
线到this.findViewId(...)
因为观点将被充气到您的布局。
在你的布局XML做:
<com.bookcessed.booksearch.SearchProviderButton
android:id="@+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/csp_tv_title"
rs:providerName="BOOKP">
<!-- include this so it's added to your custom layout view -->
<include layout="@layout/provider_view" />
</com.bookcessed.booksearch.SearchProviderButton>
provider_view变为:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/pv_rl_strings"
.
.
.
</merge>
SearchProviderButton:
public class SearchProviderButton extends RelativeLayout{
.
.
.
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//the <include> in the layout file has added these views to this
//so search this for the views
ImageView logo = this.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)this.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)this.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
.
.
.
或者你也可以通过适当充气layoutInflater.inflate(R.layout.provider_view, this, true)
在onCreate
视图。该调用会将引用的布局膨胀为通过的ViewGroup
,在这种情况下是您的自定义视图,并将虚拟的View
添加到它。然后,您可以在onFinishInflate
中修复findViewId
来电。
我想我已经尝试了这两种方式,但可能没有做到正确。我无法得到它。 – 2010-08-03 00:50:41
我已经更新了答案,以包含第一种方法所需的更改示例。 – Qberticus 2010-08-03 03:38:40
谢谢!我希望有人可以帮助你,就像你帮助我一样!谢谢你太多了。我永远无法将2和2放在一起。再次感谢! – 2010-08-03 03:58:25
要使wrap_content正常工作,您需要正确实现自定义视图的measure
和layout
函数。有关这些方法的含义的详细信息,请参见How Android Draws Views。
我的猜测是,对于您的观点getMeasureWidth()
和getMeasureHeight()
功能总是返回0。
工具文件夹中的heirarchy查看器可能会帮助您。它为您呈现视图树。它有可能是你的东西它有0宽度和高度,或类似的东西。 – 2010-08-02 22:49:16
是的,它有0宽度和0高度。我如何使wrap_content包装我的内容? (感谢这个东西是漂亮的提示) – 2010-08-02 23:04:47