Android instance-Mobile security Defender (40)-Custom Toast (ii) (layout style, background)

Source: Internet
Author: User

One

Customize the layout, background, and so on toast

Second, the Code implementation

1. Create a new layout file (Android XML file, named Phone_add_toast) in the Layout folder under the Res folder to define how the toast is to be displayed;

2, according to the design requirements of the custom toast layout for the horizontal line of the layout, width and height are the contents of the package, the left is a picture, the right is the attribution of information text (because the attribution information will vary according to the number, so you can set the ID (tv_phone_add_toast));

(1) Image using <ImageView> components, through the ANDROID:SRC attribute loading picture resources, width and height are the contents of the package;

(2) The text adopts the <TextView> component, the width height is the package content, the ID (tv_phone_add_toast) is set.

Custom Toast Layout Files:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Wrap_content"4android:layout_height= "Wrap_content"5android:orientation= "Horizontal"6android:gravity= "Center_vertical" >7 8<ImageView9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:src= "@android:d rawable/ic_menu_search"/> A      -<TextView -Android:id= "@+id/tv_phone_add_toast" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "Number Attribution"/> -  +</LinearLayout>
View Code

3. Define a member variable (named view) of a View object in the "Show Number Attribution" Service class (Showphoneaddservice) to load the layout, (to prevent duplicate names, modify the original TextView object name to Toast_phone_add);

4. In the custom Toast method, delete or comment the statement about the view before creating the window parameter Windowmanager.layoutparams object.

(1) The Toast layout file defined in 2 is loaded into the view object by the inflate (context context, int resource, ViewGroup root) method of the View object (view). The parameter context is a contextual (this is here), and resource is the parent container (null here) for the layout file resource that needs to be loaded id,root;

(2) The View object (view) setbackgroundresource (int resid), setbackgroundcolor (int color) and other methods to load the background picture, background color and other properties, optimize the UI;

(3) The TextView object (Toast_phone_add) that displays the attribution information finds the TextView component in the custom toast layout file through the Findviewbyid (int id) method of the View object (view), and turns it on strongly;

(4) The string type parameter (Phoneadd) passed into the custom toast method is loaded by the SetText (charsequence text) of the TextView object (Toast_phone_add).

Code for loading custom layouts and related settings in the Toast method:

1 view = view.inflate (thisnull); 2         View.setbackgroundcolor (color.blue); 3         Toast_phone_add = (TextView) View.findviewbyid (r.id.tv_phone_add_toast); 4         Toast_phone_add.settext (Phoneadd);    
View Code

Android instance-Mobile security Defender (40)-Custom Toast (ii) (layout style, background)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.