先看个例子:
LinearLayout.LayoutParams lytp = new LinearLayout.LayoutParams(100,100); ImageView imageView=new ImageView(this); lytp.setMargins(100,100,0,0); imageView.setLayoutParams(lytp); ((LinearLayout) this.findViewById(R.id.layout)).addView(imageView);
上述步骤动态添加ImageView控件
这里的LayoutParams相当于子控件(例子中是ImageView控件)的属性说明包,它最后通过
imageView.setLayoutParams(lytp);
这句代码告诉父容器自身的各种属性;
实例化的对象lytp还能进行各种关于控件的设置
ZHE
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值: 1,一个确定的值; 2,FILL_PARENT,即填满(和父容器一样大小); 3,WRAP_CONTENT,即包裹住组件就好。
代码:
package com.example.administrator.test; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Window; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity{ private AnimationDrawable animationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); LinearLayout.LayoutParams lytp = new LinearLayout.LayoutParams(100,100); ImageView imageView=new ImageView(this); lytp.setMargins(100,100,0,0); imageView.setLayoutParams(lytp); ((LinearLayout) this.findViewById(R.id.layout)).addView(imageView); imageView.setImageResource(R.drawable.animation1); animationDrawable=(AnimationDrawable)imageView.getDrawable(); animationDrawable.start(); } }
布局界面的xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" android:weightSum="1"> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/imageView" android:layout_gravity="center_horizontal" android:layout_marginTop="150dp"/> </LinearLayout>
帧动画:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@mipmap/mao1" android:duration="500"></item> <item android:drawable="@mipmap/mao2" android:duration="100"></item> <item android:drawable="@mipmap/mao3" android:duration="100"></item> <item android:drawable="@mipmap/mao4" android:duration="100"></item> <item android:drawable="@mipmap/mao5" android:duration="100"></item> <item android:drawable="@mipmap/mao6" android:duration="100"></item> <item android:drawable="@mipmap/mao7" android:duration="100"></item> <item android:drawable="@mipmap/mao8" android:duration="500"></item> <item android:drawable="@mipmap/mao9" android:duration="100"></item> </animation-list>
至于动画部分详见:http://blog.csdn.net/llxlqy/article/details/52276949