java 自定义视图未出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3392351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:36:55  来源:igfitidea点击:

Custom View not appearing

javaandroidxmllayoutcustom-controls

提问by Aymon Fournier

Here is the XML for the layout in which I want my custom view to appear.

这是我希望在其中显示自定义视图的布局的 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>

Here is the custom view's XML:

这是自定义视图的 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
            android:id="@+id/pv_genre2"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/pv_genre1" />

        <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
            android:id="@+id/pv_genre4"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/pv_genre3" />

        <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
            android:id="@+id/pv_genre6"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/pv_genre5" />

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

Here is the class SearchProviderButton:

这是 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);
  }
 }


}

Here is the code for the class loading the xml that contains my custom component:

这是加载包含我的自定义组件的 xml 的类的代码:

bookPServiceProviderButton = (SearchProviderButton)findViewById(R.id.csp_spb_1);
bookPServiceProviderButton.setOnClickListener(SPBOnClickListener);
bookPServiceProviderButton.setConnector(new bookPConnector());
bookPServiceProviderButton.setVisibility(View.VISIBLE);

EDIT: After the first comment, I added this code:

编辑:在第一条评论之后,我添加了以下代码:

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

}

Now it has a width and a height, but nothing is showing up inside of it!

现在它有一个宽度和一个高度,但里面没有任何东西!

回答by Rich Schuler

Your custom layout view is not appearing because you're not putting anything into it. In your onFinishInflateyou have the line inflatedView = li.inflate(R.layout.provider_view, null);But you don't add that to your view. You have two options to add the views to your custom layout view.

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

Change your custom view to extend RelativeLayout, change the enclosing RelativeLayoutto <merge>in your provider_view.xml, and fix your findViewIdlines in to this.findViewId(...)since the views will be inflated into your layout.

更改您的自定义视图扩展RelativeLayout,改变封闭RelativeLayout<merge>您的provider_view.xml,并解决您findViewId的线路this.findViewId(...),因为意见会膨胀到您的布局。

In your layout xml do:

在您的布局 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 becomes:

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

Or you could properly inflate the view in onCreateby layoutInflater.inflate(R.layout.provider_view, this, true). That call will inflate the referenced layout into the passed ViewGroup, in this case your custom view, and add the inflated Views to it. Then you can fix up the findViewIdcalls in your onFinishInflate.

或者您可以onCreate通过layoutInflater.inflate(R.layout.provider_view, this, true). 该调用会将引用的布局膨胀到传递的ViewGroup,在这种情况下是您的自定义视图,并将膨胀的Views添加到它。然后你可以修复findViewId你的onFinishInflate.

回答by Cheryl Simon

For wrap_content to work, you need to properly implement the measureand layoutfunctions for your custom View. See How Android Draws Viewsfor details on what these methods mean.

要使 wrap_content 工作,您需要为自定义视图正确实现measurelayout函数。有关这些方法的含义的详细信息,请参阅Android Draws Views

My guess would be that getMeasureWidth()and getMeasureHeight()functions for your view are always returning 0.

我的猜测是,getMeasureWidth()getMeasureHeight()你的视图功能总是返回0。

回答by SoliQuiD

TC... Sometimes it wont show in the preview unless you run it.

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