我正在尝试创建一个计数器作为复合控件。
TallyItem.java
package com.anna.mytallykeeper;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
public class TallyItem extends LinearLayout {
private Button decrementButton;
private Button incrementButton;
private TextView descriptionText;
private TextView countText;
public TallyItem(Context context) {
super(context);
LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflate.inflate(R.layout.tallyitem_layout, this);
/*
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(service);
li.inflate(R.layout.tallyitem_layout, this, true);
*/
//Get the fields
decrementButton = (Button)findViewById(R.id.decrement);
incrementButton = (Button)findViewById(R.id.increment);
descriptionText = (TextView)findViewById(R.id.description);
countText = (TextView)findViewById(R.id.count);
decrementButton.setOnClickListener(buttonListener);
incrementButton.setOnClickListener(buttonListener);
}
public TallyItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
private OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button)v;
int count = Integer.parseInt(countText.getText().toString());
if (button.getText().equals("-")) {
count--;
} else if (button.getText().equals("+")) {
count++;
}
countText.setText("" + count);
}
};
}
tallyitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/decrement"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="-"
android:textSize="30sp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Item 1"
android:textSize="25sp"
/>
<TextView
android:id="@+id/count"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textSize="60sp"
/>
</LinearLayout>
<Button
android:id="@+id/increment"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="+"
android:textSize="30sp"
/>
</LinearLayout>
main.xaml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="@+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</merge>
我不知道为什么它不会出现在main.xml中。
感谢您的帮助。
答案 0 :(得分:1)
您的视图需要传入AttributeSet的构造函数。将构造函数更改为:
public TallyItem(Context context, AttributeSet attr) {
super(context, attr);
...
}
并使用单个参数删除构造函数。
答案 1 :(得分:0)
在main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="@+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</merge>