Diseño lineal personalizado

I'm trying to make my own LinearLayout to make it reusable and modular. first my code looked like this, which worked fine:

mLlHeader = (LinearLayout)view.findViewById(R.id.ll_header);
        mTvCrump = new TextView(mContext);
        mTvCrump.setText("> "+((Item) mNavigationHistory.get(mNavigationHistory.size()-1)).getName());
        mTvCrump.setId(mNavigationHistory.size()-1);
        mTvCrump.setClickable(true);
        mTvCrump.setOnClickListener(mBreadcrumpListener);
        mBreadcrump.add(mTvCrump);
        mLlHeader.addView(mTvCrump,mNavigationHistory.size()-1);

Now i want to make my own LinearLayout class like this:

public class Breadcrump extends LinearLayout {

    private List<? super Item> mNavigationHistory = new ArrayList<Item>();
    private List<TextView> mElements = new ArrayList<TextView>();

    public Breadcrump(Context context) {
        super(context);
    }

    public Breadcrump(Context context, AttributeSet attrs) {
    super(context, attrs);
}

    public void addElement (String name) {

    }

}

mi xml se ve así:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/cell_selected"
    android:gravity="left"
    android:orientation="vertical"
    android:padding="5dp" >

      <com.client.views.Breadcrump
        android:id="@+id/ll_breadcrump"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" /> 
</LinearLayout>

y utilícelo así:

mLlHeader = (Breadcrump)view.findViewById(R.id.ll_breadcrump);
....

EDITAR

i now added the namespace in the layout xml file where i use my custom view and a new Constructor in the view class with an attribute set. Now the app starts. But it displays only my custom view and the rest of the layout is white ?

preguntado el 05 de mayo de 13 a las 15:05

Depending on what your requirements are, you might be interested in this class: FragmentBreadCrumbs -

i had small look at this, but i thought its allready to sepcific for my usecase. -

ok i finally did it by at last set the layout sizes to "wrap_content" ^^ -

1 Respuestas

In your layout XML file, you probably have something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_header"
    ... />

Intenta cambiarlo a

<?xml version="1.0" encoding="utf-8"?>
<com.client.views.Breadcrump
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_header"
    ... />

EDIT: how to use it in your activity?

If your XML layout file is called principal.xml, entonces solo llama setContentView(R.layout.main) en la onCreate() method of your activity. Your Breadcrump gets initialized internally. Then you can get a reference to it with (Breadcrump)view.findViewById(R.id.ll_header).

contestado el 05 de mayo de 13 a las 19:05

when i do it like this i get a xml error <?xml version="1.0" encoding="utf-8"?> <com.client.views.Breadcrump android:id="@+id/ll_header" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> - Adriano Celentano

the eror is unbound prefix - Adriano Celentano

Parece que te estás perdiendo el xmlns:android="http://schemas.android.com/apk/res/android". Please take a look at the edit I made to the answer. - zbr

Ok, I have deleted it. Please check the edit where I explain how to get a reference to the Breadcrump. - zbr

No, the instance of the object is created internally. If you need a reference to it, you can get it by calling the findViewById() método. - zbr

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.