-1

I create a custom view for actionbar.When i use <inclued layout=@layout/XXXXX />is ok.However ,when i use LayoutInflater to inflate the view ,it cannot inflate it .Here is my code

public class TitleLayout extends LinearLayout {
//    private Button btn_back;
//    private Button btn_edit;
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_title,this);
    }
}

TitleLayout is my custom view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_back"
        android:layout_gravity="center"
        android:text="Back"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/text_title"
        android:text="This is a title"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:gravity="center"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/btn_edit"
        android:text="Edit"/>

</LinearLayout>

It is a simple view; When i use <inclued /> to inflate is ok. But use LayoutInflate will show nothing.

<!--<include-->
        <!--layout="@layout/view_title"/>-->
   <com.example.administrator.test.TitleLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
   </com.example.administrator.test.TitleLayout> 

really appreciate your help!!!

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • what the issue? instead of `LayoutInflater.from(context).inflate(R.layout.view_title,this);` you can call the static method [inflate](http://developer.android.com/reference/android/view/View.html#inflate(android.content.Context, int, android.view.ViewGroup)) directly – Blackbelt Sep 10 '15 at 09:37
  • It is ok.ehh...why I cannot use LayoutInflater.from(context).inflate(R.layout.view_title,this); – user5320550 Sep 10 '15 at 10:11

1 Answers1

-2

try this

public class TitleLayout extends LinearLayout  {

    public TitleLayout (Context context) {
        super(context);

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.your_layout, this);
    }
}
Android Android
  • 724
  • 1
  • 7
  • 20
  • OK!! Thank you for your help; Do you know why cannot use LayoutInflater.from(context).inflate(R.layout.view_title,this);? – user5320550 Sep 10 '15 at 10:05