android - Build Custom View with allways same Layout -
i have made own layout button. normaly create view this:
layoutinflater inflater = layoutinflater.from(getactivity()); mybutton = inflater.inflate(r.layout.mybutton, null, false);
now idea class mybutton extends view , not have write these lines each button. how can bring these 2 lines in mybutton constructor? can't write:
public mybutton(context c) { = inflater.inflate(r.layout.mybutton, null, false); }
how can build custom view has allways same layout can build new mybutton preloaded layout this:
mybutton mybutton = new mybutton();
this layout want use mybutton:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cell_shape" android:orientation="vertical" android:paddingbottom="@dimen/paddingbottom" android:paddingleft="@dimen/paddinghorizontal" android:paddingright="@dimen/paddinghorizontal" android:paddingtop="@dimen/paddingtop" > <textview android:id="@+id/fach" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="@dimen/margintop" android:text="" android:textappearance="?android:attr/textappearancemedium" android:textstyle="bold" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/klasse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="@dimen/margintop" android:text="" /> <textview android:id="@+id/raum" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:text="" /> </linearlayout>
you can still reuse button xml practically independent of layout. create button.xml
in drawable folder define button properties. example of button layout has custom custom corner radius , color gradient (you need define these colors in res/values/colors.xml
,of course):
<item android:state_pressed="true"> <shape> <gradient android:startcolor="@color/pressed1" android:endcolor="@color/pressed2" android:angle="90" /> <corners android:radius="4dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true"> <shape> <gradient android:endcolor="@color/focused1" android:startcolor="@color/focused2" android:angle="270" /> <corners android:radius="4dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startcolor="@color/default1" android:endcolor="@color/default2" android:angle="90" /> <corners android:radius="4dp" /> <padding android:left="20dp" android:top="10dp" android:right="20dp" android:bottom="10dp" /> </shape> </item>
whenever need reuse it, can create button view usual:
<button android:id="@id/button1" android:layout_width="wrap_content" android:background="@drawable/button" android:text="@string/button_text"> </button>
Comments
Post a Comment