Clever Layout Design in Android [26 android Evolution]

Source: Internet
Author: User

 

Since I learned android, I felt that controls could not overlap. For example, I could not put another TextView in an EditText. I felt that the controls had a location and could not overlap. However, when I read the Android text message source code this week, I found that the controls can be put together. This shows that Google has taken this into consideration when designing the Android language, therefore, the designer cleverly implemented this function. For example, if you can enclose an image in a TextView and place a TextView in EditText, I wrote an EditViewDemo example and placed a TextView in the lower right corner of EditText, display the number of words that can be entered. A total of 160 words can be entered. If you have any questions or want the source code, you can leave a message.

 

Effect of the program at the beginning: when you enter a letter TextView, the remaining number is displayed:

 

 

Results When multiple letters are entered:

 

 

The code is provided, and everything has to be seen in the code to tell the truth:

1. mainActivity. Code in the java class:

 

Package com.cn. daming;

Www.2cto.com

Import android. app. Activity;

Import android. app. AlertDialog;

Import android. OS. Bundle;

Import android. text. Editable;

Import android. text. TextWatcher;

Import android. view. View;

Import android. widget. EditText;

Import android. widget. TextView;

 

Public class MainActivity extends Activity {

 

Private AlertDialog dialog;

Private View show_dialog;

Private EditText edit_text;

Private TextView text_view;

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Dialog = new AlertDialog. Builder (MainActivity. this). create ();

Show_dialog = View. inflate (MainActivity. this, R. layout. edittext_dialog, null );

Edit_text = (EditText) show_dialog.findViewById (R. id. edit_text );

Text_view = (TextView) show_dialog.findViewById (R. id. text_view );

Dialog. setView (show_dialog );

Dialog. show ();

Edit_text.addTextChangedListener (new EditTextWatcher ());

}

// EditTextWatcher is listener the editText changed

Public class EditTextWatcher implements TextWatcher {

 

Public void afterTextChanged (Editable arg0 ){

}

 

Public void beforeTextChanged (CharSequence cs, int arg1, int arg2,

Int arg3 ){

Edit_text.setVisibility (View. VISIBLE );

If (edit_text.getText (). length () <160 ){

Text_view.setText ("" + (160-(edit_text.getText (). length ()));

}

Else {

Edit_text.setFocusable (true );

}

}

 

Public void onTextChanged (CharSequence cs, int arg1, int arg2,

Int arg3 ){

}

}

}

 

II. The code in edittext_dialog.xml is the key to the code design. Some Properties of RelativeLayout are cleverly used to achieve control overlap:

 

<Span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_weight = "1.0"

>

<RelativeLayout

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

>

<EditText

Android: id = "@ + id/edit_text"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_alignParentLeft = "true"

Android: layout_alignParentRight = "true"

Android: maxLines = "5"

Android: maxLength = "2000"

/>

<TextView

Android: id = "@ + id/text_view"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_alignParentRight = "true"

Android: layout_alignBottom = "@ id/edit_text"

Android: textColor = "# ffff0000"

/>

</RelativeLayout>

</LinearLayout>

</Span>

 

 

3. Code in AndroidManifest. xml:

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "com.cn. daming"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "8"/>

 

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". MainActivity"

Android: label = "@ string/app_name">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

 

</Application>

</Manifest>

Related Article

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.