Android笔记之动态改变layout中的布局

1、动态显示和隐藏控件:

layout布局:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <Button
            android:id="@+id/button1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:visibility="gone"
            android:text="立即体验" />

    </RelativeLayout>

代码设置可见:

private Button open;

open=(Button)findViewById(R.id.button1);

open.setVisibility(View.VISIBLE);//设置可见

open.setVisibility(View.GONE);//不可见

2、移动图片(指示器图片):

XML布局:(根据布局文件,cursor是在最边上的)

 <ImageView
        android:id="@+id/cursor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/a" />

代码实现imageview的移动:(设置图片的位置,以及执行动画)

private void InitImageView() {
        imageView = (ImageView) findViewById(R.id.cursor);
        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
                .getWidth();//获取图片a的宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;//获取屏幕宽度
        offset = (screenW / 3 - bmpW) / 2;
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);//平行变换
        imageView.setImageMatrix(matrix);//设置imageView的初始位置
    }
......

 Animation animation = new TranslateAnimation(startX, toX, startY, toY);
 animation.setFillAfter(true);//图片停留在动画结速位置
animation.setDuration(
300);//动画执行时间300us
imageView.startAnimation(animation);//imageView做动画移动

3、改变布局的方向:

 <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:id="@+id/linear1"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:textColor="@color/mycolor" />
    </LinearLayout>

代码改变布局:

private LinearLayout linearLayout;
...
linearLayout=(LinearLayout)findViewById(R.id.linear1);
                linearLayout.setOrientation(0);//0水平布局;1垂直布局

4、利用布局文件 / 图片 创建View

(1)布局文件:layout/tab_item_view.xml

<?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:gravity="center"
    android:orientation="vertical" >
<!-- ImageView和Textiew不必设置显示值,在代码中通过view设置 -->
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:padding="3dp" >
    </ImageView>

    <TextView
        android:id="@+id/textview"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="10sp"
        android:textColor="#ffffff">
    </TextView>

</LinearLayout>

创建View,代码部分:

private LayoutInflater layoutInflater;

layoutInflater=LayoutInflater.from(this);

View view = layoutInflater.inflate(R.layout.tab_item_view, null);
   
  // 设置一个选项卡的图片资源(包括选中与未选中的不同图片)

       ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(R.drawable.tab_home_btn);

        // 设置一个选项卡的文本
        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText("主页");

        return view;

 (2)图片文件创建View

private List<View> views = new ArrayList<View>();//Views列表,add(布局)
ImageView imaV;
imaV = new ImageView(this); imaV.setImageResource(R.id.dr1); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT); imaV.setLayoutParams(params); imaV.setScaleType(ScaleType.FIT_XY); views.add(imaV);

5、include加载布局:

在主布局文件中加入一个RelativeLayout,并在屏幕底部显示:

<include android:layout_alignParentBottom="true"
        android:id="@+id/inlude1"
android:layout_height="wrap_content" android:layout_width="wrap_content" layout="@layout/action_btns"/>

新建action_btns.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/list_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffd9d9d9"
        android:orientation="horizontal">
    <ImageButton
        android:id="@+id/list_add"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_weight="1.0"
        android:src="@drawable/ic_menu_add" />
</RelativeLayout>

 6、布局属性LayoutParams:

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
ImageView imaV;
imaV.setLayoutParams(params);

7、

原文地址:https://www.cnblogs.com/xingyyy/p/3324972.html