Android 示例, "Layout Tricks: Merging Layouts"不工作

标签 android android-layout merge

只是寻求一些帮助。如果这太模糊,请告诉我。

我正在尝试此处的“合并布局”示例: http://developer.android.com/resources/articles/layout-tricks-merge.html

我似乎无法让它工作。页面上的源代码下载不包括所有需要的文件。我在下面粘贴了一些代码,其中的 block 被注释掉了。当这些未评论时,我会收到大量错误。如果有人在我开始粘贴错误之前提出建议,那就太好了...

确定取消栏:

package com.example.android.merge;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;

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(); 
        */

    }
}

主.xml:

<?xml version="1.0" encoding="utf-8"?>

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 

        android:scaleType="center"
        android:src="@drawable/golden_gate" />

    <com.example.android.merge.OkCancelBar
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"

        android:paddingTop="8dip"
        android:gravity="center_horizontal"

        android:background="#AA000000"
        <!--
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" 
        -->
        />

</merge>

最佳答案

我查看了压缩源,但没有 res/values/attrs.xml 文件。这很奇怪。
创建 attrs.xml 文件并放入下面列出的代码:

<resources>
     <declare-styleable name="OkCancelBar">
        <attr name="okLabel" format="string" />
        <attr name="cancelLabel" format="string" />
    </declare-styleable>
</resources>

现在应该可以了,但是我没时间测试,抱歉。

关于Android 示例, "Layout Tricks: Merging Layouts"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508952/

相关文章:

java - 禁用 MultiDex Android Studio

algorithm - 合并排序如何返回值

android - 从使用 Widget.EditText 样式的 TextView 中删除下划线

通过匹配其他列,将表的某一列中的某些记录替换为另一个表

git - 我将如何在 Git 中编写预 merge Hook ?

android - 如何指定 TransitionManager.beginDelayedTransition 仅影响直接 subview

android - Google Play Developer Console 为我提供了两个不同轨道的相同选择加入网址

java - 使用 AsyncTask 用多个项目填充 ExpandableListView

Android Listview - pointToPosition 返回 invalid_position

android - 如何使用按钮单击 Android 将语音短信发送到另一个联系人号码