巴啦啦 發表於 2021-8-31 18:18:00

Android开发——UI布局

<p>AS的工程中,java中放着我们的项目代码</p>
<p>res中放的是各种资源:layout放布局,mipmap放图片,values放一些值(如颜色)</p>
<h1>&lt;1&gt;线性布局——LinearLayout</h1>
<p>1.什么是线性布局?</p>
<p>线性布局就是把孩子都摆放在同一条线上</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">&lt;<span style="color: rgba(255, 0, 0, 1)">LinearLayout</span> xmlns:app="http://schemas.android.com/apk/res-auto"<span style="color: rgba(0, 0, 0, 1)">
    xmlns:android</span>="http://schemas.android.com/apk/res/android"<span style="color: rgba(0, 0, 0, 1)">
    android:<span style="color: rgba(255, 0, 0, 1)">layout_width</span></span><span style="color: rgba(255, 0, 0, 1)">="match_parent"</span><span style="color: rgba(0, 0, 0, 1)">
    android:layout_height</span>="match_parent"&gt;
&lt;/<span style="color: rgba(255, 0, 0, 1)">LinearLayout</span>&gt;</span><br><br><span style="font-size: 18px">//这段代码中,需要注意的两点:</span><br><span style="font-size: 18px">1.将根标签设置为LinearLayout,就可以实现线性布局</span><br><span style="font-size: 18px">2.layout_width=“match_parent”这条语句意思就是这个布局的宽度width自适应它父类的宽度</span></pre>
</div>
<pre></pre>
<p>2.线性布局中各个控件摆放的方向:</p>
<p>可以通过对方向 orientation进行设置来达到改变布局中控件摆放方向的效果</p>
<p>android:orientation="vertical"(垂直排列)</p>
<p>android:orientation="horizontal"(水平排列)</p>
<p>&nbsp;</p>
<p>3.线性布局中的权重:</p>
<p>如果想让布局中的各个部分平均排列或成比例排列,就可以给每个部分设置权重</p>
<p>具体的代码为 android:layout_weight="设置的权重大小值"</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:id</span>="@+id/button"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_weight</span>="<span style="color: rgba(255, 0, 0, 1)">2</span>"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="Button" /&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:id</span>="@+id/button2"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_weight</span>="<span style="color: rgba(255, 0, 0, 1)">1</span>"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="Button" /&gt;</span><br><br><span style="font-size: 18px">//在这段代码中,就分别给两个按钮的权重设置为2和1,那么显示出来的效果就是button占了</span><br><span style="font-size: 18px">整个行的2/3,而button2占了整个行的1/3</span></pre>
</div>
<p>&nbsp;</p>
<h1>&lt;2&gt;相对布局——RelativeLayout</h1>
<p>1.相对布局也就是说要有参照物的布局</p>
<p>相对布局有两种,一种是相对父控件的,另一种是相对于同级控件的</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">&lt;<span style="color: rgba(255, 0, 0, 1)">RelativeLayout</span> xmlns:android="http://schemas.android.com/apk/res/android"<span style="color: rgba(0, 0, 0, 1)">
    android:layout_width</span>="match_parent"<span style="color: rgba(0, 0, 0, 1)">
    android:layout_height</span>="match_parent"&gt;
&lt;<span style="color: rgba(255, 0, 0, 1)">/RelativeLayout<span style="color: rgba(0, 0, 0, 1)">&gt;<br><br>//将根标签设置为RelativeLayout即可实现相对布局</span><br></span></span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>2.相对父控件的相对布局</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px"> &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_centerInParent="</span><span style="color: rgba(255, 0, 0, 1)">true"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="中间"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_alignParentRight="</span><span style="color: rgba(255, 0, 0, 1)">true"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="右上角"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_alignParentBottom="true"</span><span style="color: rgba(255, 0, 0, 1)">
      android:layout_alignParentRight="true"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="右下角"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="左上角"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_alignParentBottom="true"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="左下角"/&gt;</span><br><br><span style="font-size: 18px">//系统默认加入的控件都在父控件的左上角,所以如果要更改位置的话,就需要对每个控件进行具体</span><br><span style="font-size: 18px">的设置</span></pre>
</div>
<p>&nbsp;</p>
<p>3.相对同级控件的相对布局</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">&lt;!--这个是中间部分--&gt;
    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      <span style="color: rgba(255, 0, 0, 1)">android:id</span></span>=<span style="color: rgba(255, 0, 0, 1)">"@+id/center_button"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"
       <span style="color: rgba(255, 0, 0, 1)"> android:layout_centerInParent</span>="true"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="确定"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_centerVertical</span>="true"<span style="color: rgba(0, 0, 0, 1)">
      android:<span style="color: rgba(255, 0, 0, 1)">layout_toLeftOf</span></span>=<span style="color: rgba(255, 0, 0, 1)">"@id/center_button"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="我在中间的左边"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:<span style="color: rgba(255, 0, 0, 1)">layout_above</span></span>=<span style="color: rgba(255, 0, 0, 1)">"@id/center_button"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_centerHorizontal</span>="true"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="我在中间的顶部"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_centerVertical</span>="true"<span style="color: rgba(0, 0, 0, 1)">
      android:<span style="color: rgba(255, 0, 0, 1)">layout_toRightOf</span></span>=<span style="color: rgba(255, 0, 0, 1)">"@id/center_button"</span><span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="我在中间的右边"/&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_below</span>=<span style="color: rgba(255, 0, 0, 1)">"@id/center_button"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_centerHorizontal</span>="true"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="我在中间的底部"/&gt;</span><br><br><span style="font-size: 18px">//首先给一个参照物设置id,即给中间的那个按钮设置一个id:<span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)">android:id=<span style="color: rgba(255, 0, 0, 1)">"@+id/center_button"</span></span></span>(注意这里设置id用的是<span style="color: rgba(255, 0, 0, 1)">@+id</span>)</span><br><span style="font-size: 18px">然后,其他的每一个控件相对于它在什么位置上就写相应的语句即可(注意写的时候相对于参照物用的是</span><span style="color: rgba(255, 0, 0, 1); font-size: 18px">@id</span><span style="font-size: 18px">,没有+)</span></pre>
</div>
<p>&nbsp;</p>
<h1>&lt;3&gt;其他布局</h1>
<p>1.绝对布局——AbsoluteLayout</p>
<p>一般用在机顶盒或者手表的系统开发中,因为手表和机顶盒的分辨率都是固定的</p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">&lt;<span style="color: rgba(0, 0, 0, 1)">AbsoluteLayout
    xmlns:android</span>="https://schemas.android.com/apk/res/android"<span style="color: rgba(0, 0, 0, 1)">
    android:layout_width</span>="match_parent"<span style="color: rgba(0, 0, 0, 1)">
    android:layout_height</span>="match_parent"&gt;

    &lt;<span style="color: rgba(0, 0, 0, 1)">Button
      android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
      android:text</span>="New Button"<span style="color: rgba(255, 0, 0, 1)">
      android:layout_x="92dp"
      android:layout_y="117dp"</span>/&gt;
&lt;/AbsoluteLayout&gt;</span><br><br><span style="font-size: 18px">//绝对布局就是通过layout_x和layout_y的值来确定具体的位置</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>2.表格布局——TableLayout</p>
<p>可以用来做类似计算器的按钮</p>
<p>&nbsp;</p>
<p>3.帧布局——FrameLayout</p>
<p>一般用在播放器的界面中</p>
<p>&nbsp;</p>
<h1>&lt;4&gt;开发中常用的单位</h1>
<p>1. px——像素</p>
<p>图片的最小单位就是像素,在开发中不建议使用(除非是手表或机顶盒这种固定像素屏幕的设备),因为如果使用px作为开发单位,那么在不同屏幕大小的手机上跑出来的效果就可能不相同</p>
<p>&nbsp;</p>
<p>2.dp——独立密度,与像素无关(也叫dip——Density independent pixels)</p>
<p>适配屏幕的单位,在开发中使用</p>
<p>具体的标准是,以160dpi为基准,在这个条件下1dp=1px,其他屏幕大小进行比例变化即可</p>
<p>(例如:</p>
<p>小米2s的屏幕dpi = 342,那么在标准屏幕上显示1dip的大小为1像素,在小米2s的屏幕上显示出来的像素就是 342/160 * 1 = 2.1375像素</p>
<p>)</p>
<p>&nbsp;</p>
<p>3.sp——字体单位</p>
<p>主要用来处理字体大小</p>
<p>&nbsp;</p>
<h1>&lt;5&gt;Android屏幕适配</h1>
<p>1.屏幕尺寸:</p>
<p>屏幕尺寸就是屏幕斜对角的距离,单位是英寸,一英寸约等于2.54厘米</p>
<p>&nbsp;</p>
<p>2.屏幕分辨率:</p>
<p>分辨率就是屏幕横向和纵向上的像素点个数,单位是像素(px),例如320*480</p>
<p>&nbsp;</p>
<p>3.屏幕像素密度:</p>
<p>dpi(dots per inch),简单来说就是一英寸有多少个像素点</p>
<p>如何计算dpi呢?</p>
<p>(例如有一部手机,屏幕尺寸4.3英寸,分辨率1280*720</p>
<p>——首先通过勾股定理计算出对角线上的像素点的个数 1468</p>
<p>——然后dpi=对角线上的像素点/屏幕尺寸 即 1468/4.3=342 dpi</p>
<p>)</p>
<p>&nbsp;</p>
<p>4.如何支持各种屏幕尺寸:</p>
<p>dp是支持屏幕适配的,根据屏幕的大小不同而显示的内容的像素大小也不同</p>
<p>通过以下几个方面动态地改变内容的大小:</p>
<p>——wrap_content(包裹内容)</p>
<p>——match_parent (填充父控件,也可以理解为自动匹配父控件)</p>
<p>——weight (权重)</p>
<p>&nbsp;</p>
<p>5.面试中关于屏幕适配的问题如何回答?</p>
<p>——在安卓App开发中,使用图片素材是必须的,图片的适配,可以通过做多套图片,放置于不同图片资源文件夹中来解决 </p>
<p>当然,相同的图片,名字要相同,系统会根据不同屏幕尺寸加载不同大小的图片 </p>
<p>其实,在实际开发中,大部分情况只做一套图片,部分图片需要做多套,比如说启动界面之类的</p>
<p>为什么只做一套呢?一方面是为公司节省成本,另外一方面是如果多套图片,会导致App过于臃肿,上线时整体size比较大,用户下载的话需要耗费较多的流量</p>
<p>——通过设置dimens.xml文件进行适配</p>
<p>在不同屏幕尺寸的dimens.xml文件里设置特定的高度宽度值,系统会根据屏幕的尺寸去读取不同的尺寸值</p>
<p>——布局文件的适配,这个在横竖屏幕切换方面使用比较多</p>
<p>因为横屏和竖屏的宽度改变了,而布局也相应地改变才更好看,这样有必要去写两套布局,系统会根据屏幕的横竖屏状态來选择显示的布局</p>
<p>因为在横竖屏幕切换之后,activity的生命周期会改变,会在onCreate的时候加载布局,当然,里面的数据是不变的</p>
<p>——使用权重进行适配,在写布局的时候经常使用权重去进行屏幕尺寸的适配,因为权重是一点的,也就是控件占的份额是一定的,但大小会根据屏幕尺寸的大小进行改变,这样的就可以达到适配的效果</p>
<p>——还可以通过代码去实现屏幕适配</p>
<p>通过代码动态地获取到屏幕的宽度,对单位进行换算,比如像素与dip的转换,或者相反,我们还可以动态地计算比例來确定控件在当前屏幕尺寸的大小</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/danile97/p/15211653.html
頁: [1]
查看完整版本: Android开发——UI布局