Example analysis of dynamic usage of Android control _android

Source: Internet
Author: User
Tags dateformat

The examples in this article describe the dynamic usage of the Android control. Share to everyone for your reference. Specifically as follows:

Typically, the interface layouts in Android are set in XML.
That is, in a program, you can't change the number of elements on the interface,
such as chat session interface

When someone is talking about adding a textview, this is dynamically adding controls, which cannot be laid out in the XML beforehand!

But it's okay that Android uses controls and it's not just XML.

The following code is the Java program for the dynamic production control

Package com.fetion.android;
Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.text.Layout;
Import Android.text.format.DateFormat;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.view.ViewGroup.LayoutParams;
Import android.widget.*;
Import Java.util.Calendar; /** * Test dynamic use of Android controls * @author Gaolei by 20090827/public class fetion2009 extends activity {/** The activity is the created. * * ProgressBar PB;
  Progress bar control, but take it out is for controllable, dynamically change its progress//chat The background of the dialogue is the interval of the private static final int[] bg = {color.white, color.gray};  private static int bgindex=0;
  The background of the chat dialogue the current color should be the index value in BG//The following layout parameter identifies the height of the current control fill_parent= occupies all the parent controls, wrap_content= only wraps the contents of the control//and other functions such as the left and right margins, here we use the default Private Linearlayout.layoutparams lp_ff = new Linearlayout.layoutparams (layoutparams.fill_parent, LayoutParams.FILL_
  PARENT); Private Linearlayout.layoutparams LP_FW = new Linearlayout.layoutparams (layoutparams.
  Fill_parent, layoutparams.wrap_content); Private Linearlayout.layoutparams LP_WW = new Linearlayout.layoutparams (layoutparams.wrap_content, LayoutParams.WRAP
  _content);
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Chat dialog windows need to scroll ScrollView sv = new ScrollView (this);
    Sv.setlayoutparams (LP_FF); LinearLayout layout = new LinearLayout (this); Linear layout Mode layout.setorientation (linearlayout.vertical); The control is arranged vertically layout.setbackgroundcolor (0XFF00FFFF);
    Set a special color for the layout board, which can check if there is a wrong color in our session! Enrich the chat page, also in passing test page scrolling effect, add 10 Duplicate dialog content for (int i=0; i<10; i++) {setsendmsg (layout, this, Getcurrcolor (), i+ "Chat content here ...
    " );
    //Send file effect 1, Ring progress bar, also progressbar default effect Setsendfile (layout, this, Getcurrcolor (), "my photos. jpg"); Send file effect 2, moment line progress bar, is also ProgressBar style set to Style= "? Android:attr/progressbarstylehorizontal" effect setSendFile2 (layout, this
    , Getcurrcolor (), "my photos. jpg"); for (int i=0; i<10; i++) {setsendmsg (layout, this, Getcurrcolor (), i+ "Chat content here ...
    " );  } sv.addview (layout);   Adding a linear layout to the ScrollView Setcontentview (SV);  Set the current page to ScrollView}/** * Get current chat white background value * @return Current Chat The background value of the dialogue * * Private int Getcurrcolor () {return
  bg[(++bgindex)% bg.length]; /** * Dynamically add a chat content * here in order to simplify programming put someone's words and content into a textview, you can split into 2 TextView according to the design document, set the font, etc. * @param layout TextView control To add to the target layout * @param context building the view control must be a view control's environment * @param the background color of the Bgcolur textview control * @param MSG Textvie W control to be realistic text content * * private void Setsendmsg (LinearLayout layout, context, int bgcolur, String MSG) {Textvie  W TV = new TextView (context); Ordinary chat//Get a global calendar instance to get the current system time and format it into hours: minute form, only for testing, where the time should be provided by other programs Tv.settext ("someone says: [" +dateformat.format ("kk:m
    M ", calendar.getinstance ()) +"]\n "+msg);
    Tv.setbackgroundcolor (Bgcolur);
  Layout.addview (TV); /** * Dynamically add a session entry to send a file * here because the level of the progress bar and the Cancel button is sentIts way, so need to add a linearlayout * @param layout want to add to the target layout * @param context constructs the view control's necessary parameters both the view control's environment * @param bgcol Your control's background color * @param MSG control to realistic text content * * private void Setsendfile (LinearLayout layout, context, int Bgcolu
    R, String fileName) {//Tell SB. [TIME]//To send the file information to setsendmsg to draw it!
    Setsendmsg (layout, context, Bgcolur, "sending" +filename);
    The horizontal arrangement of 2 controls requires a linearlayout, which is arranged horizontally by default linearlayout mylayout = new LinearLayout (context);
    The background color of this linearlayout control needs to be set, or it will show the color of the main linearlayout, that is, 0xff00ffff Mylayout.setbackgroundcolor (Bgcolur);
    Dynamically create a ProgressBar, add the default attributes to the Mylayout ProgressBar PB = new ProgressBar (context);
    Pb.setlayoutparams (LP_WW);
    Mylayout.addview (PB);
    Create a button dynamically, add the default attribute to the Mylayout button BT = New button (context);
    Bt.setlayoutparams (LP_WW);
    Bt.settext ("cancellation");
    Mylayout.addview (BT);
  Add the horizontal layout of the LinearLayout and its inside as all controls to the main layout layout.addview (mylayout); }/** * Dynamically adds aSession entry for sending a file * but in order to protect the ProgressBar and button's background to meet the design requirements, add a linearlayout and set its background color * @param layout to add to the target layout * @param co NTEXT construct the view control must have parameters both the view control's environment * @param the background color of the Bgcolur control * @param MSG control to realistic text content * * private void Setsendfile 2 (linearlayout layout, context, int bgcolur, String fileName) {setsendmsg (layout, context, Bgcolur, "sending"
    +filename);
    LinearLayout mylayout = new LinearLayout (context);
    Mylayout.setbackgroundcolor (Bgcolur); Mylayout.setorientation (linearlayout.vertical);//The control is perpendicular to it, the default is the horizontal//progressbar the default style is the doughnut type, where her style needs to be set to horizontal ( Horizontal line) PB = new ProgressBar (context,null,android).
    R.attr.progressbarstylehorizontal);
    Pb.setlayoutparams (LP_FW); Pb.setprogress (45); Set 1th progress to pb.setsecondaryprogress (0);
    Here we do not need 2nd progress, so for 0 Mylayout.addview (PB);
    Button BT = New button (context);
    Bt.setlayoutparams (LP_WW);
    Bt.settext ("cancellation");
    Mylayout.addview (BT);
  Layout.addview (mylayout); } @OvErride public boolean onKeyDown (int keycode, keyevent event) {log.d ("OnKeyDown:", "keycode=" + keycode + "Keyev")
    Ent= "+ event);
      Switch (keycode) {case KeyEvent.KEYCODE_DPAD_UP:break;
      Case KeyEvent.KEYCODE_DPAD_DOWN:break;
      Case Keyevent.keycode_dpad_left://Right left button can control the first progress of the increase or decrease pb.setprogress (pb.getprogress ()-5);
      Break
      Case KeyEvent.KEYCODE_DPAD_RIGHT:pb.setProgress (pb.getprogress () +5);
      Break
      Case KeyEvent.KEYCODE_DPAD_CENTER:break;
    Case KeyEvent.KEYCODE_0:break;
  Return Super.onkeydown (KeyCode, event);

 }
}

I hope this article will help you with your Android program.

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.