Android 安卓系统示例;布局技巧:合并布局“;不起作用
只是想寻求帮助。请让我知道,如果这是太模糊 我正在尝试下面的“合并布局”示例: 我似乎无法让它发挥作用。页面上的源代码下载不包括所需的所有文件。我在下面粘贴一些代码,注释掉了块。当这些未被评论时,我收到大量错误。如果在我开始粘贴错误之前有人有建议,那就太好了 OkCancelBar:Android 安卓系统示例;布局技巧:合并布局“;不起作用,android,android-layout,merge,Android,Android Layout,Merge,只是想寻求帮助。请让我知道,如果这是太模糊 我正在尝试下面的“合并布局”示例: 我似乎无法让它发挥作用。页面上的源代码下载不包括所需的所有文件。我在下面粘贴一些代码,注释掉了块。当这些未被评论时,我收到大量错误。如果在我开始粘贴错误之前有人有建议,那就太好了 OkCancelBar: package com.example.android.merge; import android.content.Context; import android.content.res.TypedArray;
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();
*/
}
}
main.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>
它现在应该可以工作了,但我没有时间测试它,对不起。在该示例中缺少几个文件。即:
在布局文件夹下:
它应该有main.xml、okcancelbar.xml和okcancelbar_button.xml。
在“值”文件夹下:
它应该有attrs.xml
示例文章中提供了main.xml和okcancelbar.xml的内容。
okcancelbar_button.xml需要将一个按钮定义为:
<?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="wrap_content"
android:layout_height="wrap_content">
</Button>
并且attrs.xml应提供标签定义:
<resources>
<declare-styleable name="OkCancelBar">
<attr name="okLabel" format="string" />
<attr name="cancelLabel" format="string" />
</declare-styleable>
</resources>
然后一切都会好起来。pawelzieba's&x-ray的答案是正确的。也看看这个修改。
将以下代码放入okcancelbar.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/okcancelbar_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
<Button
android:id="@+id/okcancelbar_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
</LinearLayout>
你能把错误贴出来吗?当试图确定问题时,这会有很大帮助。您想将压缩后的文件共享到其他地方供我下载吗?页面显示“错误404未找到文件”。谢谢,.zip也丢失了这些文件,okcancelbar_button.xml和okcancelbar.xml。@您好!您能告诉我在okcancelbar.xml中应该做什么吗?您能告诉我它会对我有帮助吗?这太迟钝了,以至于官方博客中的源示例无法工作。。。。
public class OkCancelBar extends LinearLayout
{
Context mContext;
public OkCancelBar(Context context, AttributeSet attrs)
{
super(context, attrs);
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER);
setWeightSum(1.0f);
mContext = context;
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.button)).setText(text);
((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
text = array.getString(R.styleable.OkCancelBar_cancelLabel);
if (text == null) text = "Cancel";
//((Button) findViewById(R.id.button)).setText(text);
((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
Button btnCancel = (Button) findViewById(R.id.okcancelbar_cancel);
btnCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(mContext, "Cancel is Clicked...", Toast.LENGTH_LONG).show();
}
});
Button btnOk = (Button) findViewById(R.id.okcancelbar_ok);
btnOk.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(mContext, "OK is pressed...", Toast.LENGTH_LONG).show();
}
});
array.recycle();
}
}