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

本文介绍了如何在Android中动态地显示和隐藏控件,移动图片,改变布局方向,以及利用布局文件和图片创建View。通过示例代码详细展示了包括设置控件可见性、ImageView动画、LinearLayout布局方向改变、使用LayoutInflater创建View等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

layout布局:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true" >

android:id="@+id/button1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:visibility="gone"

android:text="立即体验" />

代码设置可见:

private Button open;

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

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

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

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

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

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、改变布局的方向:

android:layout_width="match_parent"

android:orientation="vertical"

android:id="@+id/linear1"

android:layout_height="match_parent" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

android:textColor="@color/mycolor" />

代码改变布局:

private LinearLayout linearLayout;

...

linearLayout=(LinearLayout)findViewById(R.id.linear1);

linearLayout.setOrientation(0);//0水平布局;1垂直布局

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

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:orientation="vertical" >

android:id="@+id/imageview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="false"

android:padding="3dp" >

android:id="@+id/textview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="10sp"

android:textColor="#ffffff">

创建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 views = new ArrayList();//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,并在屏幕底部显示:

android:layout_width="wrap_content"

layout="@layout/action_btns"/>

新建action_btns.xml文件:

android:id="@+id/list_layout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ffd9d9d9"

android:orientation="horizontal">

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" />

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值