Can any one explain this codes to me? Creating a custom layout control

妖精的绣舞 提交于 2019-12-08 04:10:58

问题


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();
    }}

回答1:


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




回答2:


The above is almost enough to get turn the messed-up MergedLayout.zip into a usable project.

There were only 2 details I found to fix:

  1. In layout/okcancelbar_button.xml (the name of course ends with .xml),
    substitute wrap_content for fill_parent. With fill_parent one of the buttons covers the other.

  2. The file src/com/example/android/merge/R.java is included in the MergedLayout.zip

It should be deleted



来源:https://stackoverflow.com/questions/6411610/can-any-one-explain-this-codes-to-me-creating-a-custom-layout-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!