您的位置:首页 > 移动开发 > Android开发

android 自定义组合控件

2015-03-20 13:31 302 查看
项目中要用到一个界面 是在一个圆上 ,或者一个圆形矩形上显示数字 ,于是用到了组合控件,其实也和简单的东西

布局如下

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:id="@+id/iv_background"

        android:contentDescription="@null" />

    <TextView

        

        android:id="@+id/tv_number"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:padding="3dp"

        android:text="11" />

</FrameLayout>

只是自定义控件的时候 有点小问题

package com.example.customimagetext;

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ImageText extends LinearLayout {

    private Context context;

    public ImageText(Context context, AttributeSet attrs) {

        super(context, attrs);

        this.context = context;

        initview();

    }

    public ImageText(Context context) {

        this(context, null);

        this.context = context;

        initview();

    }

    private void initview() {

        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.imagetext, this);

        tvText = (TextView) view.findViewById(R.id.tv_number);

        ivImageView = (ImageView) view.findViewById(R.id.iv_background);

    }

    private TextView tvText;

    private ImageView ivImageView;

    

    public void setText(String s){

        tvText.setText(s);

    }

    

    public void setImageBack(int id){

        ivImageView.setBackgroundResource(id);

    }

}

这句要写在俩个参数的构造方法中  

其它的用法啥的 都是正常的使用

<RelativeLayout 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" >

    <com.example.customimagetext.ImageText

        android:id="@+id/it_content"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</RelativeLayout>

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {

            getSupportFragmentManager().beginTransaction()

                    .add(R.id.container, new PlaceholderFragment()).commit();

        }

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        if (id == R.id.action_settings) {

            return true;

        }

        return super.onOptionsItemSelected(item);

    }

    /**

     * A placeholder fragment containing a simple view.

     */

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {

        }

        @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container,

                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container,

                    false);

            ImageText ivImageText = (ImageText) rootView.findViewById(R.id.it_content);

            ivImageText.setText("1");

            ivImageText.setImageBack(R.drawable.mes_prompt_1);

            return rootView;

        }

    }

}

表示不能理解为什么一个构造函数的方法没有调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息