主要還是在製作 9-Patch 圖片,程式只是例行作業。
NinePatchBtnActivity
public class NinePatchBtnActivity extends Activity implements OnClickListener { private boolean isNormal; private List<Button> btnList = new ArrayList<Button>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button normalBtn = (Button) this.findViewById(R.id.normal); Button widthBtn = (Button) this.findViewById(R.id.width); Button heightBtn = (Button) this.findViewById(R.id.height); this.btnList.add(normalBtn); this.btnList.add(widthBtn); this.btnList.add(heightBtn); for (Button btn : this.btnList) { btn.setOnClickListener(this); } } @Override public void onClick(View v) { TextView tv = (TextView) this.findViewById(R.id.tv); if (this.isNormal) { this.setBackgroundResource(R.drawable.bg); tv.setText("9-Patch"); this.isNormal = false; } else { this.setBackgroundResource(R.drawable.bg_n); tv.setText("Normal"); this.isNormal = true; } } private void setBackgroundResource(int resId) { for (Button btn : this.btnList) { btn.setBackgroundResource(resId); } } }main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Normal" /> <Button android:id="@+id/width" android:layout_width="100px" android:layout_height="wrap_content" android:gravity="center" android:text="Width" /> <Button android:id="@+id/height" android:layout_width="wrap_content" android:layout_height="100px" android:gravity="center" android:text="Height" /> </LinearLayout>
沒有留言:
張貼留言