Custom view does not swell

I am trying to create my custom view via xml, but my view is not displayed on the screen, it is not bloated. My custom xml view:

<?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/profileSwitcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imgAdvertise" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@android:drawable/btn_minus" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > <TextView android:id="@+id/txtAdvertise" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="asdas" /> </LinearLayout> </ViewSwitcher> 

I have my custom View class as:

 public class MyCustomView extends View{ TextView textAdvertise; ImageView imageAdvertise; View view; ViewSwitcher switcher ; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (layoutInflater != null) { view = layoutInflater.inflate(R.layout.main, null); } initialize(); } public void initialize() { // TODO Auto-generated method stub textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise); imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise); switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher); startAnimation(); } public void startAnimation() { // TODO Auto-generated method stub new Thread() { public void run() { for (;;) { try { Thread.sleep(2000); hRefresh.sendEmptyMessage(5); } catch (Exception e) { } } } }.start(); } Handler hRefresh = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 5: switcher.showNext(); // To go back to the first view, use switcher.showPrevious() break; default: break; } } }; } 

and in my basic XML layout, where I need to show the View , there is one button, and my View like:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="0dp" android:text="Button" android:layout_weight="1"/> <com.example.viewswitcher.MyCustomView android:id="@+id/MyView" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> 

Can someone please tell me what is going on?

+6
source share
3 answers

You will not see anything, because MyCustomView has no content as you wrote it. First of all, I would use one of the ViewGroups children instead of the View (e.g. LinearLayout , RelativeLayout , etc.):

 public class MyCustomView extends LinearLayout { TextView textAdvertise; ImageView imageAdvertise; View view; ViewSwitcher switcher ; public MyCustomView(Context context, AttributeSet attrs) { super(context); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (layoutInflater != null) { view = layoutInflater.inflate(R.layout.main, this, true); } initialize(); } public MyCustomView(Context context, AttributeSet attrs, int theme) { super(context, attrs, theme); } // ... 
+9
source

Use this to inflate the layout. Edit

  View headerView = View.inflate(this, R.layout.layout_name, null); 

This will return the parent layout of your xml file, after which you can use it directly, or you can find any other view using

Edit

 headerView.findViewById(R.id.view_id); 

hope it works

+3
source

I think you do not need a custom view. Perhaps you should inherit the Fragment insted of View class.

Look here for the implementation of your own fragment.

0
source

Source: https://habr.com/ru/post/915162/


All Articles