使用LayoutParams设置布局

1、public static class

ViewGroup.LayoutParams

extends Object

java.lang.Object

   ↳android.view.ViewGroup.LayoutParams

Class Overview

LayoutParams are used by views to tell their parents how they want to be laid out.

LayoutParams是ViewGroup的一个内部类, 每个不同的ViewGroup都有自己的LayoutParams子类

DEMO:

//创建一个线性布局    
private LinearLayout mLayout;    
mLayout = (LinearLayout) findViewById(R.id.layout); 
//现在要往mLayout里边添加一个TextView    TextView textView = new TextView(Activity01.this);    
textView.setText("Text View " );    
//这里是设置 这个textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里边设置是一样的如    
/**<TextView    
android:layout_width="fill_parent"    
android:layout_height="wrap_content"    
android:text="Text View"/>*///第一个参数为宽的设置,第二个参数为高的设置。    
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(    
LinearLayout.LayoutParams.FILL_PARENT,    
LinearLayout.LayoutParams.WRAP_CONTENT    
);    
//调用addView()方法增加一个TextView到线性布局中    
mLayout.addView(textView, p);    
//比较简单的一个例子


LayoutParams继承于Android.View.ViewGroup.LayoutParams.
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。

可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。

但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT
3,WRAP_CONTENT

2、addRule()

public class

RelativeLayout

extends ViewGroup

java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.widget.RelativeLayout
java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.widget.RelativeLayout

void android.widget.RelativeLayout.LayoutParams.addRule(int verb, int anchor)

public void addRule (int verb, int anchor) 设置控件的相对位置

Added in API level 1

Adds a layout rule to be interpreted by the RelativeLayout. Use this for verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean value (VISIBLE).

Parameters
verbOne of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.
anchorThe id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.

package sunny.example.layoutparamstaddrule;import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;public  class TestView extends RelativeLayout{private LayoutParams mLayoutParams_1,mLayoutParams_2;Button mButton;TextView mTextView;public TestView(Context context) {super(context);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public void init(){mLayoutParams_1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);mLayoutParams_1.addRule(RelativeLayout.ALIGN_TOP); addView(mButton,mLayoutParams_1);mLayoutParams_2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);mLayoutParams_2.addRule(RelativeLayout.BELOW, TRUE);mTextView.setText("Hey");addView(mTextView,mLayoutParams_2);}
}


Thanks to 泡在网上的日子

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348801.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

记录意外的开关选项

Java开发人员可以做很多事情来使自己的生活以及维护该代码的其他人的生活更加轻松。 在本文中&#xff0c;我将探讨开发人员可以采用的一种非常简单的方法&#xff0c;以使每个人都更轻松。 这篇文章的要点对于每个阅读它的人来说似乎都是显而易见的&#xff0c;但是我发现这样…

View的绘制

1、当测量好一个View后就可以重写onDraw()方法&#xff0c;并在Canvas对象上绘制所需的图形。 public class Canvas extends Objectjava.lang.Object ↳android.graphics.Canvas Public ConstructorsCanvas()Construct an empty raster canvas.Canvas(Bitmap bitmap)Construc…

ViewGroup的测量及绘制

1、ViewGroup的测量public abstract class ViewGroup extends Viewimplements ViewManager ViewParent java.lang.Object ↳android.view.View ↳android.view.ViewGroup Class Overview A ViewGroup is a special view that can contain other views (called children.)…

ejb 2.0 3.0_定义EJB 3.1视图(本地,远程,无接口)

ejb 2.0 3.0这篇文章将讨论使用批注定义EJB视图的可能方法&#xff08;最后我将只提到使用EJB部署描述符&#xff09;。我将重点介绍最新的EJB 3.1视图&#xff0c;这些视图将省略旧的本地&#xff0c;远程和本地接口。 因此&#xff0c;我们可以选择&#xff1a; 远程业务界面…

View的事件分发机制简述

要分析的对象就是MotionEvent&#xff0c;点击事件的事件分发其实就是对MotionEvent事件的分发过程&#xff0c;当MotionEvent产生后&#xff0c;系统需要把这个事件传递给一个具体的View&#xff0c;这个传递过程就是分发过程。这个过程由三个很重要的方法共同完成&#xff1a…

ListView的使用用ViewHolder提升效率

public class ListView extends AbsListViewjava.lang.Object ↳android.view.View ↳android.view.ViewGroup ↳android.widget.AdapterView<T extends android.widget.Adapter> ↳android.widget.AbsListView ↳android.widget.ListView Class Ove…

通过投影增强数据模型

介绍 数据模型可能很棘手。 建模可能会更加困难。 有时候&#xff0c;应该放入数据库表中的信息不一定是我们要在每段代码中使用的信息。 和其他许多次一样&#xff0c;Spring来了。 一个称为投影的小功能可以帮助我们在普通界面中仅用几行数据映射数据。 在本文中&#xff0…

ListView常用属性、方法

1、设置item间分割线&#xff0c;分割线高度 android:divider"android:color/darker_gray" android:dividerHeight"10dp" 分割线不仅仅可以设置为一个颜色&#xff0c;也可以设置为一个图片资源。 分割线设置为透明&#xff1a; android:divider"…

具有弹性效果的ListView

Android默认的ListView在滚动到顶端或底端&#xff0c;没有很好的提示&#xff0c;下面通过源码来实现弹性效果。 ListView中有一个控制滑动到边缘的处理方法&#xff1a; protected boolean overScrollBy(int deltaX, int deltaY,int scrollX, int scrollY,int scrollRangeX,…

rememberme多久_使用Spring Security添加RememberMe身份验证

rememberme多久我在“ 向Jiwhiz博客添加社交登录”一文中提到&#xff0c;RememberMe功能不适用于Spring Social Security。 好吧&#xff0c;这是因为该应用程序现在不通过用户名和密码对用户进行身份验证&#xff0c;并且完全依靠社交网站&#xff08;例如Google&#xff0c;…

Java 9:对可选的增强

之前 &#xff0c;我写过Java 8中引入的Optional类&#xff0c;该类用于对可能不存在的值进行建模并减少可能引发NullPointerException的位置的数量。 Java 9向Optional添加了三个新方法&#xff1a; 1. ifPresentOrElse 新ifPresentOrElse方法允许你执行一个动作&#xff0c…

电脑还原Mac地址

之前修改过电脑Mac&#xff0c;现在与手机Mac冲突&#xff0c;导致手机无法连接校园网。 控制面板——》打开网络和共享中心——》选择更改适配器设置 本地连接——》右键选择属性 上图选择配置 高级——》在下拉菜单中选择网络地址——》右边选为“不存在” 之后本地连接会自…

聊天ListView使用ViewHolder

聊天界面会展示至少两种布局&#xff0c;即收到消息和发送消息。这样一个ListView与平时使用的ListView的最大不同&#xff0c;在于它有两个不同的布局&#xff1a;收到的布局和发送的布局。需要利用Adapter实现这样的效果。即需要在获取布局的时候判断该获取哪个布局。 public…

动态改变ListView布局

在getView()时&#xff0c;通过判断选择加载不同的布局。 点击某个item的时候&#xff0c;变为foucus状态&#xff0c;其他的items还原。 下面用两个方法给item设置两个不同的布局。 //foucus状态&#xff0c;显示一个ImageViewprivate View addFocusView(int i) {ImageView…

jpa 关系拥有方_JPA:确定关系的归属方

jpa 关系拥有方使用Java Persistence API&#xff08;JPA&#xff09;时&#xff0c;通常需要在两个实体之间创建关系。 这些关系是通过使用外键在数据模型&#xff08;例如数据库&#xff09;中定义的&#xff0c;而在我们的对象模型&#xff08;例如Java&#xff09;中则使用…

JDBC连接备忘单

抽象 这是常见数据库的JDBC连接的快速参考。 我似乎必须大量查找此信息&#xff0c;因此我认为最好将所有参考资料放在一个地方。 德比 <dependency><groupId>org.apache.derby</groupId><artifactId>derbyclient</artifactId><version>1…

获取View坐标

滑动一个View&#xff0c;即移动一个View&#xff0c;改变其当前所处的位置&#xff0c;通过不断改变View的坐标实现滑动的效果。 1、Android坐标系&#xff1a;坐标原点在屏幕左上角。 public void getLocationOnScreen(int[] location) Computes the coordinates of this vi…

李宏毅 课程打包_按功能而不是按层打包课程

李宏毅 课程打包大多数企业Java应用程序在设计上都有一些相似之处。 这些应用程序的打包通常由它们使用的框架&#xff08;如Spring&#xff0c;EJB或Hibernate等&#xff09;驱动。或者&#xff0c;您可以按功能对打包进行分组。 像任何其他有关建模的项目一样&#xff0c;这也…

error inflating class binaryXML LayoutParams addRule()

报出异常的原因是由于少添加了构造方法&#xff0c;三个构造方法需要写完整&#xff0c;不能只写一个。参数为(Context, AttributeSet)&#xff0c;其中第二个参数用来将xml文件中的属性初始化。 自定义控件若需要在xml文件中使用&#xff0c;就必须重写带如上两个参数的构造方…

AppD方法:Java 9支持

通过从应用程序中学习企业APM产品&#xff0c;发现更快&#xff0c;更高效的性能监控。 参加AppDynamics APM导览&#xff01; 阅读有关Java 9模块化功能带来的挑战以及AppDynamics保持在该领域的领导者的严格要求的更多信息。 我们很高兴宣布Java 17全面支持&#xff0c;这是…