2013-05-04 32 views
0

我已經爲Button創建了自己的佈局。 Normaly我這個創建一個視圖:構建與allways相同的自定義視圖佈局

LayoutInflater inflater = LayoutInflater.from(getActivity()); 
myButton = inflater.inflate(R.layout.mybutton, null, false); 

現在,我的想法是,MyButton類擴展查看和我沒有寫這些線對每個按鈕。但是我怎樣才能在myButton構造函數中引入這兩行?我不能寫:

public MyButton(Context c) 
{ 
    this = inflater.inflate(R.layout.mybutton, null, false); 
} 

如何建立具有八方通相同的佈局自定義視圖這樣我就可以建立一個新的爲myButton與預裝的佈局是這樣的:

MyButton myButton = new MyButton(); 

這是佈局我想使用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> 

回答

0

我找到了一個解決方案:

public MyButton extends LinearLayout 
{ 
    public MyButton(Context context) 
    { 
     super(context); 
     LayoutInflater.from(context).inflate(R.layout.myButton, this, true); 
     //other initialization... 
    } 
} 

有了這個XML的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Own Layout --> 
</merge> 
+0

解決方案對我來說很奇怪。您正在擴展視圖組以創建視圖。後來合併,你要求android刪除這個吊墜視圖組。不是一個好設計 – Anirudh 2013-05-05 10:42:49

1

您仍然可以重複使用XML按鈕是PR實際上獨立於任何佈局。只需在可繪製文件夾中創建一個button.xml,該文件夾將定義您的所有按鈕屬性。這是按鍵佈局的一個例子,有一個自定義的自定義圓角半徑和顏色漸變(你需要在res/values/colors.xml定義這些顏色,當然):

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

每當你需要要重新使用它,您可以照常創建按鈕視圖:

 <Button 
      android:id="@id/button1" 
      android:layout_width="wrap_content" 
      android:background="@drawable/button" 
      android:text="@string/button_text"> 
     </Button> 
+0

好感謝您的,但它不可能我以爲的方式嗎?在我看來,我認爲它變得更輕鬆舒適。我可以擁有自己的公有和私有方法,所以我認爲這是一個更好的方法來構建它。也許有點像MVVM。 – Cilenco 2013-05-04 19:31:13

+0

當然可以。你只需要擴展'Button'或CompoundButton'類並設置相同的drawable即可。 – Neoh 2013-05-04 22:33:59

+0

我不明白drawable在這種情況下如何幫助我。我用我想用於myButton的佈局更新了我的第一篇文章。也許你可以看看這個。 – Cilenco 2013-05-05 07:39:44