gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:42 24 4
gpt4 key购买 nike

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

我正在尝试此处的“合并布局”示例: 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com