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;
}
}
}
表示不能理解为什么一个构造函数的方法没有调用
布局如下
<?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;
}
}
}
表示不能理解为什么一个构造函数的方法没有调用
相关文章推荐
- Android开发学习笔记-自定义组合控件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android Topbar(自定义组合控件)
- android UI进阶之自定义组合控件
- Android:自定义组合控件
- Android进阶——自定义View之重写ViewGroup组合系统控件实现自定义ToolBar模板
- Android自定义view组合控件解析
- android自定义View——组合控件 4000
- Android 自定义UI组合控件设计方法
- Android项目-自定义组合控件
- Android自定义View之组合控件 ---- LED数字时钟
- Android自定义组合控件以及使用方法_1
- 自定义组合控件(Android)
- Android自定义View 自定义组合控件
- Android开发学习笔记-自定义组合控件的过程
- android - 自定义(组合)控件 + 自定义控件外观
- Android自定义view——组合控件
- android:自定义组合控件Weight(高仿猫眼底部菜单栏)
- android 自定义组合控件总结(两个Button点击切换)
- Android wiki:06.自定义组合控件属性的步骤