android获取控件的宽高
1. 在android中listview中怎么获取条目中某个控件的宽度
先getchildAt找到这个条目,然后findviewbyid找到控件,最后getWidth就行了。
2. android 如何动态设置控件的宽度和高度
android中的控件如果在xml布局文件中把控件的layout_width和layout_height写成固定值了,好像就不能再在程序中更改该控件的高度和宽度了,不知哪位大侠有何良策可以指教一二,如 xml文件内容如下: <LinearLayout android:id="@id/dialog_bottom_neutral" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="center"<Buttonandroid:id="@id/dialog_bottom_neutral_button" android:layout_width="80.0dip" android:layout_height="28.0dip" android:background="@drawable/dlg_button" android:gravity="center" android:singleLine="true" android:text="mid" android:textColor="@drawable/dlg_button_text_color" android:textSize="14.0sp" / 小弟在程序中使用button.width和button.height设置没用,用LayoutParmas设置也没用
3. android 在onCreate中获得控件的大小
这个方法并不是适合所有场景,这个方法获取的宽度是minWidth参数设置的大小和background指定背景宽度,这两个宽度的最大值,高也是如此,也就是说如果View的xml中没有两个参数中的其中一项,那么这个方法测量的宽高也是为0的,这个方法测量的并不是获取xml中设置的android:layout_height android:layout_width的值,为什么这么说了,看源码:
imageView.measure(w, h); -->调用View的measure方法-->onMeasure()方法,onMeasure源码:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
onMeasure->setMeasuredDimension()->getDefaultSize()>getSuggestedMinimumHeight()
这个是源码onMeasure中方法调用过程,逆向分析方法源码:
getSuggestedMinimumHeight():
protected int getSuggestedMinimumHeight() {
return (mBackground == null) ? mMinHeight : max(mMinHeight,mBackground.getMinimumHeight());
}
如果背景为空,那么就取mMinHeight的值,如果背景不为空就取max(mMinHeight,mBackground.getMinimumHeight())背景高度和mMinHeight最大值
接下来获取建议值完毕后查看getDefaultSize的源码:
//第一个参数是getSuggestedMinimumHeight方法获取的建议值 第二个参数是系统计算得出的宽高规格是MeasureSpec值,也就是measure(w,h)中的w或者h,
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
//int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//规格模式不就是上面的:View.MeasureSpec.UNSPECIFIED
int specMode = MeasureSpec.getMode(measureSpec);
//规格模式不就是上面的 0
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {//这里是什么了?View.MeasureSpec.UNSPECIFIED理解吧
case MeasureSpec.UNSPECIFIED://
//result就是getDefaultSize要返回的值,根据switch判读getDefaultSize返回的是什么了
//不就是方法的第一个形参吗,这个形参不就是宽高建议值吗
//也就是max(mMinHeight,mBackground.getMinimumHeight());
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
好了,现在就是setMeasuredDimension方法了,源码:
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
boolean optical = isLayoutModeOptical(this);
if (optical != isLayoutModeOptical(mParent)) {
Insets insets = getOpticalInsets();
int opticalWidth = insets.left + insets.right;
int opticalHeight = insets.top + insets.bottom;
measuredWidth += optical ? opticalWidth : -opticalWidth;
measuredHeight += optical ? opticalHeight : -opticalHeight;
}
mMeasuredWidth = measuredWidth;//这个是这个方法要注意的值
mMeasuredHeight = measuredHeight;//同上
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
这个代码好长啊,好多东西,要关注的就是注释的代码,上面要注意的两行代码有什么用了
你再看一个方法的源码你就是知道了,getMeasureWidth()与getMeasureHeight():
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
public final int getMeasuredHeight() {
return mMeasuredHeight & MEASURED_SIZE_MASK;
}
这两个方法不就是返回调用measure测量的宽高吗?不就是上面两行注意的代码的值吗
现在回答你的问题:
这是代码,我想问makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)中的一个参数为什么是0,什么意思?
第一个参数本应该是系统测量该View后得到的规格值(MeasureSpec),本来这个measure是由系统测量完宽高后自动调用,我们这里只是做了系统即将要做的事情而已,那么这个参数为什么是0了,既然我们要通过这个方法测量View的宽高,不就是怕系统还没有自动调用这个方法前调用getMeasureWidth/Height方法而没法获得导致取值为0 ,也就是我们默认调用这个方法就是系统没有对该View绘制,就直接调用了measure方法,所以也就是宽高为0咯,其实这
makeMeasureSpec的第一个参数设置什么都无所谓啦,因为最后取得值也不是第一个参数设置的值,我觉得我的表达好绕啊,不过要是你对measure的绘制机制的源码很熟悉的话,应该是没问题的,这里我推荐你去看(谷歌的小弟)csdn的博客里面有完整的源码分析,你要以前看的不是很懂,去看看他写的博客应该会有点启发
4. android中,如何点击一个按钮,控制一个已经存在的控件的高度宽度
点击事件你会把,这个就不说了,设置宽高
就是
button.setLayoutParams(new
LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
LayoutParams具体导入什么包就得看你外面用的是什么。
还有你说的循环添加监听那个,你首先几个按钮的id设置为
这种。。button1,button2...
for(int
i
=
0;
i
<
size;
i++){
Button
button
=
findViewById(R.id.button1
+
i);
button.setOnClickListener();
}
你这个
for(int
i=1;i<8;i++){
Button
heaterB=(Button)findViewById(R.id.heaterB+i);
地方错了
应该是
for(int
i=0;i<8;i++){
Button
heaterB=(Button)findViewById(R.id.heaterB1+i);
5. android开发中遇到一个问题,进入界面时候,listview是隐藏的,但是我要动态这是item中控件的宽度
这样看你listview显示item的布局,如果整个布局下只有一个Listview,TextView和EditText是一上一下的话,直接match_parent即可(最好截个图或把布局代码发出来)。获取listview的item的高度和宽度我提供一个代码:
java">publicstaticvoidgetListItemWH(ListViewlistView){
try{
//获取ListView对应的Adapter
ListAdapterlistAdapter=listView.getAdapter();
if(listAdapter==null){
return;
}
intlen=listAdapter.getCount();//item总数
for(inti=0;i<len;i++){
ViewlistItem=listAdapter.getView(i,null,listView);
if(null!=listItem){
listItem.measure(0,0);//计算子项View的宽高
inth=listItem.getMeasuredHeight();//每一个item的高度
intw=listItem.getMeasuredHeight();//每一个item的宽度
Log.d("test","h="+h";w="+w);//打印看看
}
}
listView.getDividerHeight();//获取子项间分隔符占用的高度
}catch(Exceptione){
}
}
6. android 代码里怎么设置控件的宽度
你可以通过findViewById() 先获得这个控件剩下的就是你去调用这个控件的方法在你的代码里面设置控件的宽高了。
7. android下,如何实现动画效果那样修改控件的宽高
创建一个线程修改控件大小相关数据,handler设置控件LayoutParmas
8. 怎么获取android.support.design.widget.FloatingActionButton控件的大小
Android动态改变View控件大小的方法:
1、声明控件参数获取对象 LayoutParams lp;
2、获取控件参数: lp = 控件id.getLayoutParams();
3、设置控件参数:如高度。 lp.height -= 10;
4:、使设置生效:控件id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());