Java 自定义视图不显示

Java 自定义视图不显示,java,android,xml,layout,custom-controls,Java,Android,Xml,Layout,Custom Controls,下面是我希望自定义视图显示的布局的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:id="@+id/widget273

下面是我希望自定义视图显示的布局的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:id="@+id/widget273"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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>
编辑:在第一条注释之后,我添加了以下代码:

@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);

}

现在它有一个宽度和一个高度,但里面什么都没有显示

要使wrap_内容正常工作,您需要为自定义视图正确实现
测量
布局
功能。有关这些方法的详细信息,请参见


我的猜测是,视图的
getMeasureWidth()
getmeasurewhight()
函数总是返回0。

您的自定义布局视图不会出现,因为您没有在其中放入任何内容。在您的
onFinishInflate
中有一行
inflatedView=li.inflate(R.layout.provider\u视图,null)但您不会将其添加到视图中。有两个选项可以将视图添加到自定义布局视图中

将自定义视图更改为扩展
RelativeLayout
,将提供程序中的封装
RelativeLayout
更改为_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>

提供者视图变为:

<?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>


TC。。。有时,除非您运行它,否则它不会显示在预览中。

在“工具”文件夹中找到的Heriarchy查看器可能会在这里帮助您。它为您可视化了视图树。可能你的视图的宽度和高度都是0,或者类似的东西。是的,它的宽度和高度都是0。如何使包装内容包装我的内容?(顺便说一句,这件事很漂亮,谢谢你的提示)我想我已经尝试了两种方法,但可能做得不对。我不明白。我已经更新了答案,包括第一种方法所需更改的示例。谢谢!我希望有人能像你刚才帮助我一样帮助你!非常感谢你。我自己永远也无法把2和2放在一起。再次感谢!你知道为什么我的自定义视图非常高吗?可能与测量有关?我试着在第一个下面再放一个,但是你添加的onMeasure在屏幕外删除了,因为这不是真正的问题。我还没有试过运行你的布局,所以我不确定它应该有多高。看起来它不应该超过30像素。没错!!我有一个正确的xml结构,但直到第一次运行时才显示。一定是安卓工作室的bug
@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);

}
<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>
<?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>
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();
 }
 .
 .
 .