android - 通过膨胀布局创建自定义 View ?

标签 android xml view

我正在尝试创建一个自定义 View 来替换我在多个地方使用的特定布局,但我正在努力这样做。

基本上,我想替换这个:

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

通过这个:

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

所以,我不想要一个自定义布局,我想要一个自定义 View (这个 View 应该不可能有 subview )。

唯一可以从一个 MyQuantityBox 实例更改为另一个实例的是标题。我非常希望能够在 XML 中指定这一点(就像我在最后一行 XML 中所做的那样)

我该怎么做?我应该将 RelativeLayout 放在/res/layout 中的 XML 文件中,然后在 MyBoxQuantity 类中对其进行扩充吗?如果是,我该怎么做?

谢谢!

最佳答案

有点老了,但我想根据 chubbsondubs 的回答分享我的做法: 我使用 FrameLayout (参见 Documentation ),因为它用于包含单个 View ,并将 xml 中的 View 膨胀到其中。

代码如下:

public class MyView extends FrameLayout {
    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

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

    public MyView(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.my_view_layout, this);
    }
}

关于android - 通过膨胀布局创建自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4328838/

相关文章:

java - 使用 Window 的 FLAG_FULLSCREEN 时 ICS 和 Jelly Bean 之间的不同行为

python - 有没有办法通过表格获取模型ID?

android - 为多个主机和自签名证书自定义 HostNameVerifier

android - 在谷歌地图中更改 autocomplete_fragment 的图标

java - Properties.loadFromXML NullPointerException 与 jUnit

java - java中输入字符串无法解析为整数

Android添加多张图片到drawable res文件夹

php - FCM 成功但 android 设备未收到通知

xml - 如何使用 R 的 XML 库进行 xpath 查询?

objective-c - iOS中心底部位置 View