美文网首页Android 开发
2019-08-20 Android 线性布局介绍-Linear

2019-08-20 Android 线性布局介绍-Linear

作者: 可乐W | 来源:发表于2019-08-20 21:54 被阅读0次

    1.LinearLayout (线性布局)

     它是一个视图组,所有的子元素都是单个方向的:

    vertically(垂直) or horizontally(水平),要么都是垂直,要么都是水平。

    属性(attribute):android:orientation 。

    属性:android:layout_weight="1".

    Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

    layout_width="wrap_content"只是在子LinearLayout布局下使用,并且有的也建议改成0dp.

    (1)新建项目-androidLinearLayout

    (2)修改layout的布局文件activity_main.xml:

       默认下的布局是RelativeLayout,(相对布局),

       然后就改成LinearLayout.

    (3)编写一个实例-用户登录界面(采用LinearLayout布局)

    效果图:

    设计界面(可视化界面):

    AVD上的效果图:

     思路:从大到小的布局,均采用LinearLayout线性布局。

                  再添加控件,进行里面的细节修改。

    layout的布局文件activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical" #大布局-垂直

        tools:context="com.example.andriod_linearlayout.MainActivity" >

        <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"  #根据内容设置

        android:background="#CCCCCC" #背景颜色

        android:orientation="horizontal">  #小布局-水平

            <TextView

                android:id="@+id/textView1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/username" /> #在values 的strings.xml下定义,消除下警告(黄色波浪线)

            <EditText

                android:id="@+id/editText1"

                android:layout_width="0dp"(消除黄色波浪线)

                android:layout_height="wrap_content"

                android:layout_weight="1"(“权重”)

                android:ems="10" >

                <requestFocus />

            </EditText>

    </LinearLayout>

        <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#CCCCCC"

        android:orientation="horizontal">

            <TextView

                android:id="@+id/textView2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/password" />

            <EditText

                android:id="@+id/editText2"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight="0.76"

                android:ems="10"

                android:inputType="textPassword" />

            </LinearLayout>

      <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#CCCCCC"

        android:gravity="center"

        android:orientation="horizontal">

          <Button

              android:id="@+id/button1"

              android:layout_width="wrap_content"

              android:layout_height="wrap_content"

              android:layout_weight="1"#权重 

              android:text="@string/login" />

          <Button

              android:id="@+id/button2"

              android:layout_width="wrap_content"

              android:layout_height="wrap_content"

              android:layout_weight="1"

              android:text="@string/reset" />

          </LinearLayout>

    </LinearLayout>

    strings.xml 文件:

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

    <resources>

        <string name="app_name">andriod_linearlayout</string>

        <string name="hello_world">Hello world!</string>

        <string name="action_settings">Settings</string>

        <string name="username">用户名:</string>

        <string name="password">密码:</string>

        <string name="login">登录</string>

        <string name="reset">取消</string>

    </resources>

    相关文章

      网友评论

        本文标题:2019-08-20 Android 线性布局介绍-Linear

        本文链接:https://www.haomeiwen.com/subject/buhpsctx.html