由多个视图自定义视图 [英] Custom view made of multiple views

查看:156
本文介绍了由多个视图自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组的意见我想永远在一起使用。这样的一个例子可以是这样的:

I have a set of views I want to always use together. An example of this may be something like:

<LinearLayout>
    <TextView />
    <EditView />
</LinearLayout>

该文本视图是一个提示,并编辑视图就是答案。我想给这个组合的名称,并能够使用该名称弹出到XML。我很乐意为它是一个自定义视图,这样我就可以把它很好的一类,并为其创造各种实用功能。有没有什么办法可以做到这一点?我知道我可以继承的LinearLayout并在Java code动态创建的孩子,但我失去了通过XML轻松地进行更改的能力。有没有更好的途径?

The text view is a prompt, and the edit view is the answer. I would like to give this combination a name, and be able to use that name to pop it into xml. I'd love for it to be a custom view so I can put it nicely in a class and create various utility functions for it. Is there any way I can do that? I know I could subclass LinearLayout and create the children dynamically in the java code, but that loses me the ability to easily make changes via xml. Is there a better route?

是的,我也有地方我想这样做,这是更多地参与不仅仅是提示。

And yes, I also have places I want to do this which are more involved than just prompts.

推荐答案

这个例子是一个自定义的水平数选择器窗口小部件,但它是同一个概念。

This example is for a custom horizontal number picker widget, but it's the same concept.

首先创建布局组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

   <Button
       android:id="@+id/btn_minus"
       android:layout_width="50dp"
       android:layout_height="wrap_content"
       android:text="-" />

   <EditText
       android:id="@+id/edit_text"
       android:layout_width="75dp"
       android:layout_height="wrap_content"
       android:inputType="number"
       android:gravity="center"
       android:focusable="false"
       android:text="0" />

   <Button
       android:id="@+id/btn_plus"
       android:layout_width="50dp"
       android:layout_height="wrap_content"
       android:text="+" />
</LinearLayout>

然后创建一个类的小工具

Then create a class for your widget

public class HorizontalNumberPicker extends LinearLayout {
    public HorizontalNumberPicker(Context context, AttributeSet attrs) {
         super(context, attrs);
         LayoutInflater inflater = LayoutInflater.from(context);
         layoutInflater.inflate(R.layout.horizontal_number_picker, this);
     }
 }

添加Java code这个类,做任何你想要的部件做,那么你可以只包含小部件在XML布局,是指它在你的活动

Add the java code in this class that does whatever you want the widget to do, then you can just include the widget in your xml layout and refer to it in your Activity

<com.example.HorizontalNumberPicker
     android:id ="@+id/horizontal_number_picker"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" />

您可以看到在本教程的更多细节。
另请参见:<一href="http://developer.android.com/guide/topics/ui/custom-components.html#compound">http://developer.android.com/guide/topics/ui/custom-components.html#compound

You can see more details in this tutorial.
Also see: http://developer.android.com/guide/topics/ui/custom-components.html#compound

这篇关于由多个视图自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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