relativelayout

时间:2024-03-18 03:35:46编辑:coo君

linearlayout和relativelayout的区别

LinearLayout(线性布局)
每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局,只有一行,每一个元素依次向右排列。
RelativeLayout(相对布局)
相对布局可以理解为某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below、 android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh


linearlayout和relativelayout的区别

LinearLayout 布局:
故名思义,线性布局,组件以垂直或水平方向线性排列。android.widget.LinearLayout 有个继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例调用 LinearLayout.addView 就可以实现“线性布局”。
首先我们需要定义一个LinearLayout的布局参数params,如下:
方式一:指定高、宽
LinearLayout.LayoutParams params=new LinearLayout.LayoutParam(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)
方式二:指定高、宽、权重
LinearLayout.LayoutParams params=new LinearLayout.LayoutParam(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,weight)
其中LayoutParams中的两个参数分别为:子控件的高、宽。
通过设定LinearLayout.LayoutParams.gravity=Gravity.NO_GRAVITY/Gravity.TOP/Gravity.BOTTOM/Gravity.LEFT/Gravity.RIGHT
用来指定设置组件相对于容器本身的位置了。通过addView(child, params)增加子控件。

参考:http://blog.sina.com.cn/s/blog_892efd6001010vvj.html


RelativeLayout布局:
顾名思义,就是以“相对”位置/对齐为基础的布局方式。android.widget.RelativeLayout 有个继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例调用 RelativeLayout.addView 就可以实现“相对布局”。
首先我们需要定义一个 RelativeLayout的布局参数relLayoutParams,如下:
RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParam(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)
其中LayoutParams中两个参数分别为:子控件的宽(width),子控件的高(height),除了可以为LayoutParams.FILL_PARENT(android.view.ViewGroup.LayoutParams)等系统常量外还可以是数值,比如400;
下面这里就是重点了:
通过LayoutParams的 addRule方法来额外的添加别的规则了,android.widget.RelativeLayout.LayoutParams.addRule(int verb, int anchor),
其中 anchor 参数指定可以是 View 的 id(“相对于谁”)、RelativeLayout.TRUE(启用某种对齐方式)或者 是-1(应用于某些不需要 anchor 的 verb)[因为 RelativeLayout.TRUE的值为 -1 ,所以-1或者RelativeLayout.TRUE都是可以的]、是 0 (不启用这个规则)
其中 verb 参数指定相对的“动作”;
(1)如果是相对于父控件的相对布局的话 anchor 参数可以不用或者设置为-1或者RelativeLayout.TRUE ,
(2)如果是相对于级别和自己同一级的控件的话参数设置应该是 view 的id ,
(3)如果参数设置为 0 的话,则表示这个规则不会运用到该控件的布局中,当是相对于本身的父控件的时候这个参数可以省略。
比如:
relLayoutParams.addRule(RelativeLayout.ABOVE,imageViewId.getId())
子控件相对于控件:imageViewId在其的上面
relLayoutParams.addRule(RelativeLayout.BELOW ,imageViewId.getId())
子控件相对于控件:imageViewId在其的下面
relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT ,-1) 与
relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT ,RelativeLayout.TRUE) 与
relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT )
表示的是一样的表示子控件在父控件的右边
(
relLayoutParams.setMargins(arg0, arg1, arg2, arg3)或者 relLayoutParams.topMargin=5 等等离某元素的左、上、右、下的距离单位


如何在代码中设置RelativeLayout的宽高

设置RelativeLayout的宽高:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_item);
relativeView=(RelativeLayout)findViewById(R.layout.grid_item);
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
relativeView.setLayoutParams(new RelativeLayout.LayoutParams(100,200));
}
}, 1000); ///延时,或不可行
}
可行方法:
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)mScrollView.getLayoutParams();
linearParams.height = middleHeight;
mScrollView.setLayoutParams(linearParams);
设置的控件RelativeLayout 中,如果是 LinerLayout,或者是FrameLayout,做相应改动即可!


怎么在代码中设置RelativeLayout的宽高

  LayoutParams params = new LayoutParams(width,height);
  然后使用RelativeLayout relativeLayout.setLayoutparams(params);就可以在程序中控制RelativeLayout的宽和高了。
  不过需要注意的是,LayouParams的导入包的问题,导入的时候会提示很多,一定要选择RelativeLayout.LayoutParams这个才正确,否则出现ClassCastExcetpion异常.


如何在Relativelayout中动态设置控件位置

用LayoutParams:

RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.screen);//screen是一个RelativeLayout 布局的id

ImageView imgApple2 = new ImageView(MainActivity.this);
imgApple2.setBackgroundColor(Color.parseColor("#ffb6b4"));

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
layoutParams.topMargin=8;
layoutParams.leftMargin=8;
layoutParams.rightMargin=8;
layoutParams.bottomMargin=8;

insertLayout.addView(imgApple2,layoutParams);


android开发 如何在相对布局中动态添加控件

首先setMargin方法不是RelativeLayout的方法,而是RelativeLayout.LayoutParams的方法。
你应该这麼用:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
TextView mView = new TextView(this);
mView.setId(2);
mView.setText("this is a test text!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
-2, -2);
// layoutParams.setMargins(100, 100, 100, 100);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
layout.addView(mView, layoutParams);
上例是将一个TextView添加到RelativeLayout的底部。你可以把注释行取消掉,把下一行注释,再看下效果。


linearlayout和relativelayout的区别

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失。因此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加上边框高度)。LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或者左对齐)。
RelativeLayout相对布局允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。


android LinearLayout 里面的东西怎么换行

由于前段时间项目中使用到了自动换行的线性布局,本来打算用表格布局在里面一个个的用Java代码添加ImageView的,但是添加的View控件是不确定的,因为得靠服务器的数据返回,就这样手动用Java代码画布局的方式就这样夭折了,因为在表哥布局中我无法确定一行显示多少个ImageView的数目,所以无法动态添加,最后自能自己去看看那种能够换行的线性布局了,线性布局比较不好的是不能自动换行,也就是当设置LinearLayout的orentation 设置为vertical 为竖直方向也就是只有一列,每行只能显示一个View或者View的子类,当设置LinearLayout的orentitation为Horizontal,LinearLayout的只能显示为一行,横向显示,当屏幕满了的时候,View控件并不会自动换行,所以我们要做的就是在LinearLayout满的时候自动换行。
需要了解的是怎么样绘制根据子控件的长宽绘制父控件的宽度与高度,所以需要传入的参数控件的高度,视图分为两种一种是View类型的,代表控件有TextView,Button,EditText 等等,还有一种是装视图的容器控件继承自ViewGroup的控件,如LinearLayout,RelativeLayout,TabHost等等控件,需要自动换行的线性布局的话,就需要根据子控件的高度与宽度,来动态加载父控件的高度与宽度,所以需要在构造函数中传入每一个子控件的固定的高度,或者是动态设置子控件的高度与宽度。
将自定义的LinearLayout 也继承自ViewGroup 并且重写抽象类ViewGrouop的几个方法:onMeasure(),onLayout(),dispathDraw() 三个方法的意思分别是:第一个onMeasure()是用来计算控件以及子控件所占用的区域,第二个onLayout()是控制子控件的换行,第三个可写可不写,主要是用来绘制控件的边框,
自定义LinearLayout的代码如下:

[java] view plaincopyprint?
package com.huanglong.mylinearlayout;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* @author huanglong 2013-5-28 自定义自动换行LinearLayout
*/
public class FixGridLayout extends ViewGroup {
private int mCellWidth;
private int mCellHeight;

public FixGridLayout(Context context) {
super(context);
}

public FixGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public FixGridLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void setmCellWidth(int w) {
mCellWidth = w;
requestLayout();
}

public void setmCellHeight(int h) {
mCellHeight = h;
requestLayout();
}

/**
* 控制子控件的换行
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cellWidth = mCellWidth;
int cellHeight = mCellHeight;
int columns = (r - l) / cellWidth;
if (columns < 0) {
columns = 1;
}
int x = 0;
int y = 0;
int i = 0;
int count = getChildCount();
for (int j = 0; j < count; j++) {
final View childView = getChildAt(j);
// 获取子控件Child的宽高
int w = childView.getMeasuredWidth();
int h = childView.getMeasuredHeight();
// 计算子控件的顶点坐标
int left = x + ((cellWidth - w) / 2);
int top = y + ((cellHeight - h) / 2);
// int left = x;
// int top = y;
// 布局子控件
childView.layout(left, top, left + w, top + h);

if (i >= (columns - 1)) {
i = 0;
x = 0;
y += cellHeight;
} else {
i++;
x += cellWidth;

}
}
}

/**
* 计算控件及子控件所占区域
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 创建测量参数
int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST);
int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST);
// 记录ViewGroup中Child的总个数
int count = getChildCount();
// 设置子空间Child的宽高
for (int i = 0; i < count; i++) {
View childView = getChildAt(i);
/*
* 090 This is called to find out how big a view should be. 091 The
* parent supplies constraint information in the width and height
* parameters. 092 The actual mesurement work of a view is performed
* in onMeasure(int, int), 093 called by this method. 094 Therefore,
* only onMeasure(int, int) can and must be overriden by subclasses.
* 095
*/
childView.measure(cellWidthSpec, cellHeightSpec);
}
// 设置容器控件所占区域大小
// 注意setMeasuredDimension和resolveSize的用法
setMeasuredDimension(resolveSize(mCellWidth * count, widthMeasureSpec),
resolveSize(mCellHeight * count, heightMeasureSpec));
// setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

// 不需要调用父类的方法
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/**
* 为控件添加边框
*/
@Override
protected void dispatchDraw(Canvas canvas) {
// 获取布局控件宽高
int width = getWidth();
int height = getHeight();
// 创建画笔
Paint mPaint = new Paint();
// 设置画笔的各个属性
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setAntiAlias(true);
// 创建矩形框
Rect mRect = new Rect(0, 0, width, height);
// 绘制边框
canvas.drawRect(mRect, mPaint);
// 最后必须调用父类的方法
super.dispatchDraw(canvas);
}

}
然后在Xml文件中引用自己定义的控件,在Java代码中调用:


[java] view plaincopyprint?
package com.huanglong.mylinearlayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
private SimpleAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FixGridLayout fixGridLayout = (FixGridLayout) findViewById(R.id.ll);
fixGridLayout.setmCellHeight(30);
fixGridLayout.setmCellWidth(100);
for (int i = 0; i < 7; i++) {
CheckBox box = new CheckBox(MainActivity.this);
box.setText("第"+i+"个");
fixGridLayout.addView(box);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}


RelativeLayout中两个控件怎么居中显示

<RelativeLayoutandroid:id="@+id/widget27"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><ImageButtonandroid:id="@+id/widget30"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_centerHorizontal="true"><ImageButtonandroid:id="@+id/widget31"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/widget30"android:layout_alignLeft="@+id/widget30">

android RelativeLayout设置其属性wrap_content后,为什么显示的布局还是撑满全屏?

检查两个地方:
是否在AndroidManifest.xml 里面设置了全屏。
你的layout 文件中,有padding="0dp"这样的设定
你的layout文件中,有其他元素 ,textview 等形式的元素 内容你设定了为 fill_parent.

解决办法:很简单,你希望显示的布局差距是多少,就在RelativeLayout 的参数里面加入 android:paddingLeft , android:paddingTop 这些参数,就可以了


Android中的wrap_content是什么意思

WRAP_CONTENT、MATCH_PARENT/FILL_PARENT属性的原理说明
① fill_parent

设置一个视图的布局为fill_parent将强制性地使视图扩展至父元素大小。

② match_parent
Android 中match_parent和fill_parent意思一样,但match_parent更贴切,于是从2.2开始两个词都可以
用,但2.3版本后建议使用match_parent。
③ wrap_content
自适应大小,强制性地使视图扩展以便显示其全部内容。以TextView和ImageView控件为例,设置为
wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。

出处:http://blog.csdn.net/qinjuning


onmeasure什么时候调用

为什么relativelayout会让子view调用2次onmeasure在程序中是根据前缀来区分各种进制数的。因此在书写常数时不要把前缀弄错造成结果不正确。2) 八进制整常数:八进制整常数必须以0开头,即以0作为八进制数的前缀。数码取值为0~7。八进制数通常是无符号数。以下各数是合法的八进制数:015(十进制为13)、0101(十进制为65)、0177777(十进制为65535);以下各数不是合法的八进制数:256(无前缀0)、03A2(包含了非八进制数码)、-0127(出现了负号)。3) 十六进制整常数:十六进制整常数的前缀为0X或0x。其数码取值为0~9,A~F或a~f。以下各数是合法的十六进制整常数:0X2A(十进制为42)、0XA0 (十进制为160)、0XFFFF (十进制为65535);以下各数不是合法的十六进制整常数:5A (无前缀0X)、0X3H (含有非十六进制数码)。


上一篇:dppb

下一篇:triplekill