Android setting中设置启动wifi,ethernet,bluetooth时需要的一种UI :
自定义UI类:
package com.example.androidinflateuidemo1;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RelativeLayout;import android.widget.Switch;import android.widget.TextView;import android.widget.Toast;public class Custome_Component extends RelativeLayout{ private Context mContext; private TextView mTitle,mSummary; private Switch mSwitch; public Custome_Component(Context context) { super(context); // TODO Auto-generated constructor stub } public Custome_Component(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public Custome_Component(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub mContext=context; LayoutInflater layoutInflater=LayoutInflater.from(context); final View mView=layoutInflater.inflate(R.layout.customeui, this); mSwitch=(Switch)mView.findViewById(R.id.switch1); mTitle=(TextView)mView.findViewById(R.id.textView1); mSummary=(TextView)mView.findViewById(R.id.textView2); mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(mContext, "switch", Toast.LENGTH_SHORT).show(); } }); } public void setTitle(String title){ mTitle.setText(title); } public void setSummary(String summary){ mTitle.setText(summary); }}
对应的XML:下面的不是手工写的,直接用布局器拖放的
主程序:
package com.example.androidinflateuidemo1;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { private com.example.androidinflateuidemo1.Custome_Component mCustomeUI; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mCustomeUI=(com.example.androidinflateuidemo1.Custome_Component)this.findViewById(R.id.custome); mCustomeUI.setTitle("customeui title"); mCustomeUI.setSummary("customeui summary"); } @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; } }