最近很多小伙伴都在问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="-2")
- Android Layout 布局文件里的 android:layout_height 等属性为什么...
- android layout_weight
- Android layout_weight不工作
- Android linearLayout 之 layout_weight 揭秘
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 等属性为什么...
有的时候,我们配置好的布局文件,在加载完成添加到我们的 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;
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);
}
}
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new FrameLayout.LayoutParams(getContext(), attrs);
}
android layout_weight
Q: 当使用 layout_weight 时,发现上一层 3 个 下一层 2 个时不对齐现象。
A: 实际上 layout_weight 为当前剩余空间的权重, 因为每一个块都有 margin,第一层有多一个, 可以修改第二层的 layout_width , 如 margin 为 5 时,修改第二层的 layout_width 为 10.
Android layout_weight不工作
预期
[全部] [我的] [按钮] [转] [这里]
我得到了什么
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>
解决方法
这里有两篇很好的博客文章解释了权重如何工作:
http://blog.stylingandroid.com/archives/297http://blog.stylingandroid.com/archives/312
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 揭秘的相关信息,可以在本站进行搜索。
本文标签: