Android dynamically adds a line feed

Source: Internet
Author: User

Android dynamically adds a line feed

Add images dynamically in the project and wrap the text. At first I wanted to use the GridView, but it was useless. Why did I forget it. Below I will record my implementation methods.

See the code xml:

 
     
      
      
  
 

Activity:

Package org.cn. ruimit. pic; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. util. arrayList; import org.cn. ruimit. r; import android. content. intent; import android. database. cursor; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. imageView. scaleType; import android. widget. linearLayout; import android. widget. linearLayout. layoutParams; import com. china. hunbohui. baseActivity; import com. china. hunbohui. IApplication;/*** @ author wangjing */public class AddPicAty extends BaseActivity {private static final int SEL_PIC = 1; private LinearLayout linearLayout = null; private ArrayList
 
  
ImageBeans; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. add_pic_lay); initView ();} private void initView () {imageBeans = new ArrayList (); linearLayout = (LinearLayout) findViewById (R. id. linearlayout); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// select Intent intent = new Intent (Intent. ACTION_GET_CONTENT); // "android. intent. action. GET_CONTENT "// intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("image/*"); startActivityForResult (intent, SEL_PIC) ;}}) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK) {if (requestCode = SEL_PIC) {Uri uri = data. getData (); String [] proj = {MediaStore. images. media. DATA}; Cursor cursor = managedQuery (uri, proj, null); // according to my personal understanding, this is to obtain the index value of the selected image int column_index = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); // move the cursor to the beginning. This is very important, and it is easy to cause excessive cursor. moveToFirst (); // obtain the image path String path = cursor Based on the index value. getString (column_index); ImageBean bean = new ImageBean (); bean. setPath (path); imageBeans. add (bean); updateLayout () ;}}/ *** update Image Layout */private void updateLayout () {LinearLayout ll_horizontal = null; linearLayout. removeAllViews (); for (int I = 0; I <imageBeans. size (); I ++) {if (I % 4 = 0) {ll_horizontal = new LinearLayout (context); ll_horizontal.setOrientation (LinearLayout. HORIZONTAL); linearLayout. addView (ll_horizontal);} ImageView img = new ImageView (context); setImgLayoutParams (img); setImageBitmap (img, imageBeans. get (I ). getPath (); ll_horizontal.addView (img);}/*** set imageview size */private void setImgLayoutParams (ImageView img) {// LayoutParams lp = (LayoutParams) img. getLayoutParams (); LayoutParams lp = new LayoutParams (0, 0); lp. width = (int) (IApplication. with-20 * IApplication. dencity-3*5 * IApplication. dencity)/4); lp. height = lp. width; lp. rightMargin = (int) (5 * IApplication. dencity); lp. bottomMargin = (int) (5 * IApplication. dencity); img. setLayoutParams (lp); img. setScaleType (ScaleType. CENTER_CROP);}/*** set image for imageview */private void setImageBitmap (ImageView img, String url) {try {FileInputStream FD = new FileInputStream (url); img. setImageBitmap (BitmapFactory. decodeStream (FS);} catch (FileNotFoundException e) {e. printStackTrace () ;}/ *** image object */class ImageBean {private String path; public String getPath () {return path;} public void setPath (String path) {this. path = path ;}}}
 

Main Learning Points: Set the image size and update the layout.






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.