2013-05-04 33 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