常見android中的style

來源:互聯網
上載者:User

標籤:android   style   

自訂對話方塊設定style

  <style name="TipsDialog" parent="@android:Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowFullscreen">true</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowIsTranslucent">false</item>         <item name="android:windowBackground">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowContentOverlay">@null</item>    </style>

  <style name="myDialogTheme" parent="android:Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">false</item>         <item name="android:windowNoTitle">true</item><!--除去title-->        <item name="android:windowContentOverlay">@null</item>         <item name="android:backgroundDimEnabled">false</item>        <item name="android:windowBackground">@null</item><!--除去背景色-->


一般我們將一些公用屬性抽象出來做為一個個style,在xml中直接引用style即可,既減少了代碼而且在style發生變化的時候一處改變所引用的地方均變化

    <style name="layout_match_parent">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">match_parent</item>    </style>    <style name="layout_wrap_content">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>    <style name="layout_weight_horizontal">        <item name="android:layout_width">0dip</item>        <item name="android:layout_height">match_parent</item>    </style>    <style name="layout_weight_vertical">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">0dip</item>    </style>

具體使用方式:例如我們需要自訂一個dialog,不需要dialog的title代碼如下:

/** * 圖片放大dialog *  * @author jone *  *         2014年5月13日 com.tiza.tree.xingchang_line.widget *  */public class ImageDialog extends Dialog implementsandroid.view.View.OnClickListener {public ImageDialog(Context context, int theme) {super(context, theme);}ImageView imageView;Button repeatButton;Button cancleButton;View rootView;LayoutInflater inflater;public static final int POSITIVE = 1;public static final int NEGATIVE = 2;public ImageDialog(Context context) {//調用自訂的style,用來去掉dialog預設的titlethis(context, R.style.MyDialog);inflater = LayoutInflater.from(context);init();wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);dm = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(dm);screenWidth = dm.widthPixels;screenHeight = dm.heightPixels;Log.i("tag", screenHeight+":"+screenHeight);}public Button getRightButton(){return repeatButton;}private void init() {rootView = inflater.inflate(R.layout.xc_img_dialog, null);setContentView(rootView);imageView = (ImageView) rootView.findViewById(R.id.xc_big_img_);repeatButton = (Button) rootView.findViewById(R.id.xc_big_left);cancleButton = (Button) rootView.findViewById(R.id.xc_big_right);repeatButton.setOnClickListener(this);cancleButton.setOnClickListener(this);}public void setImageView(BitmapDrawable drawable) {imageView.setImageDrawable(drawable);}public void setDialogLayoutParamtrs(){LayoutParams params = new LayoutParams(screenWidth-20, screenHeight-200);params.setMargins(10, 25, 10, 10);imageView.setLayoutParams(params);}public interface ImageListener {public void onButtonClick(int id);}public ImageListener listener;private DisplayMetrics dm;private WindowManager wm;private int screenWidth;private int screenHeight;public void setImageListener(ImageListener listener) {this.listener = listener;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.xc_big_left:if (listener != null) {listener.onButtonClick(POSITIVE);}break;case R.id.xc_big_right:if (listener != null) {listener.onButtonClick(NEGATIVE);}break;default:break;}}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.