android - 任何人都可以向我解释这些代码吗?创建自定义布局控件

标签 android android-relativelayout

谁能给我解释一下这些代码到底在做什么?我不明白它是如何添加两个按钮(确定和取消)的。我希望有一些按钮创建代码,如 new Button() 或类似的东西它正在访问带有 id 的按钮,但在任何 xml 文件中都没有带有这些 id 的按钮。我只能在 R 文件中看到 R.id.okcancelbar_ok 定义。

谢谢。

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

源代码: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);

这一行用布局 R.layout.okcancelbar 膨胀“this”

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

这意味着布局 okcancelbar 中有一个 id 为“okcancelbar_ok”的按钮(之前已膨胀) 接下来我们分配给它文本“Ok”

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

同上,布局okcancelbar中有一个id为“okcancelbar_cancel”的按钮

所以这段代码是: 1) 膨胀 View R.layout.okcancelbar 2) 获取 ID 为“okcancelbar_ok”的按钮(在之前的布局中声明)并将文本设置为“Ok” 3) 与按钮“okcancelbar_cancel”和文本“取消”相同

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

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

还应该有一个“values/attrs.xml”,如下所示:

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

最后“layout/okcancelbar_button”应该是这样的:

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

希望对你有帮助

关于android - 任何人都可以向我解释这些代码吗?创建自定义布局控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411610/

相关文章:

android - 相对布局底部按钮上升

android - gitignore android 包含 apk

java - 安卓 PHP 获取

java - 可以在不设置 Activity 的 ContentView 的情况下获取对 View 的引用

Android:动态添加按钮到布局

android - 我的 Android ListView 项目布局看起来很糟糕

android - 如何将图像从安卓设备上传到谷歌云存储?

android - 如何应用带边框的自定义图像 mask ?

java - 静态方法或单例,选择哪一个?

android - RelativeLayout 对齐父级 *side* + 边距 *side*