GVKun编程网logo

android – 会覆盖layout_width / layout_height值吗?(android:layout_width="-2")

7

最近很多小伙伴都在问android–会覆盖layout_width/layout_height值吗?和android:layout_width="-2"这两个问题,那么本篇文章就来给大家详细解答一下,

最近很多小伙伴都在问android – 会覆盖layout_width / layout_height值吗?android:layout_width="-2"这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android Layout 布局文件里的 android:layout_height 等属性为什么...、android layout_weight、Android layout_weight不工作、Android linearLayout 之 layout_weight 揭秘等相关知识,下面开始了哦!

本文目录一览:

android – 会覆盖layout_width / layout_height值吗?(android:layout_width=

android – 会覆盖layout_width / layout_height值吗?(android:layout_width="-2")

我有一个名为my_layout.xml的布局xml文件,其中根视图的layout_width和layout_height已指定为200px.

<com.jlee.demo.MyLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="200px"
     android:layout_height="200px">

如果我使用此xml包含在另一个xml中,并将layout_width和layout_height指定为100px.

<include layout="@layout/my_layout"
     android:layout_width="100px"
     android:layout_height="100px"/>

my_layout的实际宽度/高度是多少?

解决方法:

这个问题中最高投票的答案可以帮助你:

Does Android XML Layout’s ‘include’ Tag Really Work?

您只能覆盖以layout_开头的参数.所以在你的情况下,布局应该是100 * 100.

Android Layout 布局文件里的 android:layout_height 等属性为什么...

Android Layout 布局文件里的 android:layout_height 等属性为什么...

有的时候,我们配置好的布局文件,在加载完成添加到我们的 Activity 中后发现,并没有安装我们设置的属性

来布局,比为我们设置了 android:layout_marginTop="100dip",但是运行程序后发现一点作用都没有,相似的还有 layout_height 等以 android:layout_开头的属性设置都没有作用,这类问题以我们使用 Adapter 的作为数据源的时候作用居多,因为 Adapter 里有一个方法是 getView, 这个返回的 VIew 是一个从 XML 布局里加载的,一般如下:

if(convertView==null){
convertView=LayoutInflater.from(mContext).inflate(R.layout.main, null);
}
return convertView;
问题恰恰出在我们的 LayoutInflater.from (mContext).inflate (R.layout.main, null); 这句代码上,在使用 inflate 的时候,如果第二个参数 (View root) 为 null, 那么将不会加载你的布局文件里的最顶层的那个布局节点的布局相关配置(就是以 android:layout_开头的属性).. 我们可以看下该方法的实现来说明一下,通过查找源代码,inflate 的实现都在这个 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot) 方法里定义。。其中一段:
if (root != null) { 
if (DEBUG) { 
System.out.println("Creating params from root: " + 
root); 
} 
// Create layout params that match root, if supplied 
params = root.generateLayoutParams(attrs); 
if (!attachToRoot) { 
// Set the layout params for temp if we are not 
// attaching. (If we are, we use addView, below) 
temp.setLayoutParams(params); 
} 
}
可以看到,当 root 为 null 的时候是不会执行 params = root.generateLayoutParams (attrs); 这段代码的,这段代码就是把 xml 里的布局配置转为 LayoutParams, 换句说就是加载我们配置的布局属性,以供布局类(FrameLayout 等)在 onLayout 的时候控制 View 的大小、位置、对齐等等。。以 FrameLayout 为例,看下它的 generateLayoutParams (attrs) 方法。
public LayoutParams generateLayoutParams(AttributeSet attrs) { 
return new FrameLayout.LayoutParams(getContext(), attrs); 
}
 
很简单,构造了一个 FrameLayout.LayoutParams 类,该类集成了 MarginParams,增加了一个 gravity 对其的属性配置。。。
在这里,如果要自定义自己的 VIewroup,并且该 ViewGroup 有一些自定义控制布局的属性设置,就可以通过
集成 View.MarginParams 来扩展布局配置,然后重写 generateLayoutParams 方法,这样系统框架就会自动使用该布局读取我们在 xml 中配置的布局属性来控制我们的 VIew 的位置。。
基于以上分析,我们在使用 LayoutInflate 的 inflate 方法的时候一定要保证 root 参数不能为 null,其实这个 root 就是父 View 的意思,就是说你把 xml 转换为一个 VIew 的时候,该 VIew 的 Parent 是 root,如果你不想把该 View 添加到该 root 里,那么让第三个参数 attachToRoot 为 false,如果要添加则为 true.
说到这个问题了,其实还有一些布局,他们的参数配置要满足一定的条件才会起作用,比如 FrameLayout 里的 View,你要想它的 leftMargin 生效,必须指定它的 layout_gravity 为 left,同理 right 对应 rightMargin.top 和 bottom 也一样。。在使用时注意即可,多看看源代码。要不然就会莫名起名,不知道哪里的问题。
ViewGroup 的三条线
onMeasure 测量 View 的大小
onLayout 对 View 的布局进行控制
draw 绘制该 View,drawChild 绘制子 VIew

android layout_weight

android layout_weight

Q: 当使用 layout_weight 时,发现上一层 3 个 下一层 2 个时不对齐现象。

A: 实际上 layout_weight 为当前剩余空间的权重, 因为每一个块都有 margin,第一层有多一个, 可以修改第二层的 layout_width , 如 margin 为 5 时,修改第二层的 layout_width 为 10.

输入图片说明

Android layout_weight不工作

Android layout_weight不工作

我正在制作一个带有5个按钮的片段.当它在像平板电脑这样的大屏幕上时,我会使用一个片段将所有5个按钮放在顶部.我将所有layout_width设置为1,但按钮不会像预期的那样在屏幕上均匀分布.

预期
[全部] [我的] [按钮] [转] [这里]

我得到了什么
b1 b2 b3 b4 b5 b3和b5只是一个按钮…只是显示它打破了一个字
[全部] [我] [但] [去] [她]并将其排成一行.
         [吨] [e]

我一直在玩它并尝试不同的东西,但我不能让它改变.任何帮助表示赞赏.谢谢.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xlarge_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
    android:id="@+id/tip_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/tip_button"
    android:onClick="tipButton">
</Button>

<Button
    android:id="@+id/preference"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/settings"
    android:onClick="showPreferences">
</Button>

<Button
    android:id="@+id/rate_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/rate_button"
    android:onClick="rateButton">
</Button>

<Button
    android:id="@+id/Feedback_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/Feedback_button"
    android:onClick="FeedbackButton">
</Button>

<Button
    android:id="@+id/cancel_btn"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="@string/exit_button"
    android:onClick="cancelButton">
</Button>
</LinearLayout>

解决方法

简答:将按钮的layout_width设置为0. layout_weight用于确定如何在布局中的元素之间传播剩余空间.因此,如果未将宽度设置为零,则按钮内容的大小将影响它们的大小.

这里有两篇很好的博客文章解释了权重如何工作:

http://blog.stylingandroid.com/archives/297http://blog.stylingandroid.com/archives/312

Android linearLayout 之 layout_weight 揭秘

Android linearLayout 之 layout_weight 揭秘

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:layout_gravity="center">
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="风清扬"
              android:gravity="center"
              android:layout_weight="1.0"
              android:background="@color/blue"/>
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="无厓子"
              android:gravity="center"
              android:layout_weight="2.0"/>
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="令狐冲"
              android:gravity="center"
              android:layout_weight="3.0"
              android:background="@color/red"/>
</LinearLayout>

结果如下图:

我们今天的关于android – 会覆盖layout_width / layout_height值吗?android:layout_width="-2"的分享就到这里,谢谢您的阅读,如果想了解更多关于Android Layout 布局文件里的 android:layout_height 等属性为什么...、android layout_weight、Android layout_weight不工作、Android linearLayout 之 layout_weight 揭秘的相关信息,可以在本站进行搜索。

本文标签: