创建自定义视图通过充气的布局? [英] Create a custom View by inflating a layout?

查看:110
本文介绍了创建自定义视图通过充气的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义视图将取代一定的布局,我在多个地方使用,但我在努力这样做。

I am trying to create a custom View that would replace a certain layout that I use at multiple places, but I am struggling to do so.

基本上,我想替换这样的:

Basically, I want to replace this:

<RelativeLayout
 android:id="@+id/dolphinLine"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
    android:layout_centerInParent="true"
 android:background="@drawable/background_box_light_blue"
 android:padding="10dip"
 android:layout_margin="10dip">
  <TextView
   android:id="@+id/dolphinTitle"
   android:layout_width="200dip"
   android:layout_height="100dip"
   android:layout_alignParentLeft="true"
   android:layout_marginLeft="10dip"
   android:text="@string/my_title"
   android:textSize="30dip"
   android:textStyle="bold"
   android:textColor="#2E4C71"
   android:gravity="center"/>
  <Button
   android:id="@+id/dolphinMinusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinTitle"
   android:layout_marginLeft="30dip"
   android:text="@string/minus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
  <TextView
   android:id="@+id/dolphinValue"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_marginLeft="15dip"
   android:background="@android:drawable/editbox_background"
   android:layout_toRightOf="@+id/dolphinMinusButton"
   android:text="0"
   android:textColor="#2E4C71"
   android:textSize="50dip"
   android:gravity="center"
   android:textStyle="bold"
   android:inputType="none"/>
  <Button
   android:id="@+id/dolphinPlusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinValue"
   android:layout_marginLeft="15dip"
   android:text="@string/plus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
</RelativeLayout>

通过这样的:

By this:

<view class="com.example.MyQuantityBox"
    android:id="@+id/dolphinBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:myCustomAttribute="@string/my_title"/>

所以,我不想要一个自定义布局,我想自定义视图(它不应该是可能的这一观点有孩子)。

So, I do not want a custom layout, I want a custom View (it should not be possible for this view to have child).

这可能会改变从MyQuantityBox到另一个的一个实例的唯一的事情就是冠军。我非常希望能够在XML中指定此(像我这样在最后的XML行)

The only thing that could change from one instance of a MyQuantityBox to another is the title. I would very much like to be able to specify this in the XML (as I do on the last XML line)

我怎样才能做到这一点?我应该把RelativeLayout的一个XML文件/ RES /布局和膨胀在我的MyBoxQuantity类?如果是的话我该怎么办呢?

How can I do this? Should I put the RelativeLayout in a XML file in /res/layout and inflate it in my MyBoxQuantity class? If yes how do I do so?

谢谢!

推荐答案

是的,你可以做到这一点。 RelativeLayout的,LinearLayout中,等有意见,自定义布局是一个自定义视图。只是要考虑的,因为如果你想创建一个自定义布局,你可以。

Yes you can do this. RelativeLayout, LinearLayout, etc are Views so a custom layout is a custom view. Just something to consider because if you wanted to create a custom layout you could.

您想要做的是创建一个复合控制。您将创建RelativeLayout的子类,在code(TextView的,等等)加入我们的所有的组件,并在构造函数你可以阅读从XML传入的属性。然后,您可以通过该属性为您的标题TextView的。

What you want to do is create a Compound Control. You'll create a subclass of RelativeLayout, add all our your components in code (TextView, etc), and in your constructor you can read the attributes passed in from the XML. You can then pass that attribute to your title TextView.

<一个href="http://developer.android.com/guide/topics/ui/custom-components.html">http://developer.android.com/guide/topics/ui/custom-components.html

这篇关于创建自定义视图通过充气的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆