Android自定义UI
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:下面的不是手工写的,直接用布局器拖放的
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_toRightOf="@+id/imageView1" android:text="TextView" /> <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:text="Switch" /> </RelativeLayout>
主程序:
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; } }
运行结果:

浙公网安备 33010602011771号