android layout 按比例布局的代码

2016-02-19 10:20 7 1 收藏

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

【 tulaoshi.com - 编程语言 】

为了创建比例大小的子View,可以将LinearLayout的宽度和高度设为fill_parent, 而将子View的宽度或是高度设为0,然后为子View设置不同权重(weight) ,这样子View的大小就会权值成比例。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

本例使用横向LinearLayout,LinearLayout的android:layout_width=”match_parent”,表示将使用整个屏幕宽度。

对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextView都使用了相同的权值,因此四个TextView将会有相同的宽度。这样对于那些显示文字比较长的TextView的高度就变为多行。
代码如下:

LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

TextView
android:background=”@drawable/red”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_small”/

TextView
android:background=”@drawable/green”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_big”/

TextView
android:background=”@drawable/blue”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_small” /

TextView
android:background=”@drawable/yellow”
android:layout_width=”0dip”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_wrap”/

/LinearLayout

来源:https://www.tulaoshi.com/n/20160219/1594261.html

延伸阅读
今天在看视觉规范的时候,大高走过来说帮他们那边看一个问题。又是一个关于自适应的问题。不过,我喜欢。瞄了一下,需求是这样的:用户上传照片,照片的尺寸未知;需要生成一个预览,这个预览图要根据提供给用户预览的区域自应用,并且居中;如果图片太大,需要按比例缩放。如下图。   瞄了一下,居中可以用 text-align:center; 来实...
标签: Web开发
使用CSS布局非常简单,如果你习惯使用tables布局,可能开始时有点困难,但其实很容易,事实上只是观念的不同。 你需要把网页的每个部分看成独立的块,你可以绝对或相对定位块。 Positioning 定位 positon属性可以指定元素为absolute,relative,static或是fixed。 static是元素默认属性,按HTML出现的先后顺序。 relativ...
android:gravity:设置的是控件自身上面的内容位置 android:layout_gravity:设置控件本身相对于父控件的显示位置。 看下如下代码段 代码如下: ?xml version="1.0" encoding="utf-8"? !-- android:gravity设置了按钮上面的文字的显示位置,而android:layout_gravity设置了按钮在布局中的显示位置。-- LinearLayout xmlns:android="http:/...
标签: autocad教程
AutoCAD的2000版本以后对布局(layout)的功能有了较大的改进,后来的2002以及2004tulaoshi都沿用了这种方便的功能。但是还是有很多的用户在模型空间中完成打印,一旦掌握了布局的特殊功能,相信很多朋友会放弃在模型空间中打印。 一、布局(图纸空间)与模型空间的比较 模型选项卡可获取无限的图形区域。在模型空间中,按 1:1 ...
标签: Web开发
CSS两列布局,右侧固定,左侧自适应宽度div style="width:90%; margin:0 auto;"    div style="width:200px; float:right;"这是右侧的内容/div    div style=" margin-right:210px;"这是左侧的内容,自适应宽度/div /divCSS两列布局,左侧固定,右侧自适应宽度div style="width:90%;...

经验教程

333

收藏

11
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部