开发者

Android compound control won't display

开发者 https://www.devze.com 2023-03-26 17:04 出处:网络
I\'m trying to create a counter as a compound control. TallyItem.java package com.anna.mytallykeeper; import android.content.Context;

I'm trying to create a counter as a compound control.

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" />
</LinearL开发者_如何学JAVAayout>
</merge>

I am at a loss as to why it won't show up in main.xml.

Thanks for any help.


your view need a constructor with the AttributeSet passed in. Change your constructor to this:

public TallyItem(Context context, AttributeSet attr) {
    super(context, attr);
    ...
}

and get rid of the constructor with the single argument.


try this in your 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消