任何一个可以解释这个codeS给我吗?创建自定义布局控制 [英] Can any one explain this codes to me? Creating a custom layout control

查看:123
本文介绍了任何一个可以解释这个codeS给我吗?创建自定义布局控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释我这些codeS他们在做什么是什么呢?我不明白它是如何将两个按钮(确定和取消)。我期待一些按钮创建code像新按钮()或类似的东西,这是访问与标识的按钮,但没有与任何XML文件这些ID没有按钮。我可以看到 R.id.okcancelbar_ok 认定中的R档。

感谢。

原始来源: http://developer.android.com/resources /articles/layout-tricks-merge.html

来源$ C ​​$ C: http://progx.org/users/Gfx/安卓/ MergeLayout.zip

 公共类OkCancelBar扩展的LinearLayout {
    公共OkCancelBar(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        setOrientation(水平);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0F);
                LayoutInflater.from(上下文).inflate(R.layout.okcancelbar,这一点,真正的);
                TypedArray阵列= context.obtainStyledAttributes(ATTRS,R.styleable.OkCancelBar,0,0);
                字符串文本= array.getString(R.styleable.OkCancelBar_okLabel);
        如果(文字== NULL)文本=确定;
        ((按钮)findViewById(R.id.okcancelbar_ok))的setText(文本)。
                文字= array.getString(R.styleable.OkCancelBar_cancelLabel);
        如果(文字== NULL)文本=取消;
        ((按钮)findViewById(R.id.okcancelbar_cancel))的setText(文本)。
                array.recycle();
    }}


解决方案

  LayoutInflater.from(上下文).inflate(R.layout.okcancelbar,这一点,真正的);

这条线膨胀本的布局R.layout.okcancelbar

 ((按钮)findViewById(R.id.okcancelbar_ok))。的setText(文本)

这意味着有一个按钮ID为在布局okcancelbar(早期充气)okcancelbar_ok
接下来我们分配给它的文本确定

 ((按钮)findViewById(R.id.okcancelbar_cancel))的setText(文本)。

上述相同,有一个与在布局IDokcancelbar_cancel的按钮okcancelbar

所以这code做的事:
1)膨胀的观点R.layout.okcancelbar
2)获得ID为okcancelbar_ok按钮(在previous布局声明)和文本设置为确定
3)同上用按钮okcancelbar_cancel和文本取消

布局/ okcancelbar.xml的布局应该是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<合并的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <包括
        布局=@布局/ okcancelbar_button
        机器人:ID =@ + ID / okcancelbar_ok/>    <包括
        布局=@布局/ okcancelbar_button
        机器人:ID =@ + ID / okcancelbar_cancel/>
< /合并>

有也应该是一个值/ attrs.xml,看起来像:

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
<申报-设置样式名称=OkCancelBar>
    < attr指示NAME =okLabel格式=字符串/>
    < attr指示NAME =cancelLabel格式=字符串/>
< /申报,设置样式>
< /资源>

和最后的布局/ okcancelbar_button应该是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
<按钮
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID /按钮
  机器人:方向=垂直
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>
< /按钮>

希望它能帮助

can anyone explain me these codes what are they doing exactly? I could not understand how it is adding two buttons (OK and Cancel). I expect some button creation code like new Button() or something like that It is accessing the buttons with id but there are no button with these ids in any xml file. I can just see the R.id.okcancelbar_ok defination in R file.

Thanks.

Original source : http://developer.android.com/resources/articles/layout-tricks-merge.html

Source Code : http://progx.org/users/Gfx/android/MergeLayout.zip

public class OkCancelBar extends LinearLayout {
    public OkCancelBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0f);
                LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
                TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
                String text = array.getString(R.styleable.OkCancelBar_okLabel);
        if (text == null) text = "Ok";
        ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
                text = array.getString(R.styleable.OkCancelBar_cancelLabel);
        if (text == null) text = "Cancel";
        ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
                array.recycle();
    }}

解决方案

LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);

this line inflate "this" with the layout R.layout.okcancelbar

((Button) findViewById(R.id.okcancelbar_ok)).setText(text)

it means that there is a button with id "okcancelbar_ok" in the layout okcancelbar ( inflated earlier) Next we assigned to it the text "Ok"

((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);

same as above, there is a button with id "okcancelbar_cancel" in the layout okcancelbar

So this code do : 1) inflate the view R.layout.okcancelbar 2) get the button (declared in the previous layout) with id "okcancelbar_ok" and set the text to "Ok" 3) idem with the button "okcancelbar_cancel" and text "Cancel"

The "layout/okcancelbar.xml" layout should be like that :

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <include
        layout="@layout/okcancelbar_button"
        android:id="@+id/okcancelbar_ok" />

    <include
        layout="@layout/okcancelbar_button"
        android:id="@+id/okcancelbar_cancel" />
</merge>

There also should be a "values/attrs.xml" that looks like :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OkCancelBar">
    <attr name="okLabel" format="string"/>
    <attr name="cancelLabel" format="string"/>
</declare-styleable>
</resources>

And finally the "layout/okcancelbar_button" should looks like :

<?xml version="1.0" encoding="utf-8"?>
<Button
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/button"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">    
</Button>

Hope it helps

这篇关于任何一个可以解释这个codeS给我吗?创建自定义布局控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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