UI组件之线性布局LinearLayout Android Studio 找不到R文件解决方法汇总

常用属性

1 android:id

例如:@+id/height

“@”符号是提示XML解析器应该把后面的字符串解析成标识符号。

“+”符号代表将添加一个标识符号。

“id/”表示这个标识符号回被归类在“id”下面。

"height"是这个界面主见的“android:id”。

以后的程序中,会使用“R.id.height”来取得这个界面组件。因此"@+id/height"的意思是我们在此创建了名为“height”的标识符,可以通过这个标识符来控制所对应的界面组件,“R”类会自动配置一个地址给这个界面组件。“R”类的内容,这可以通过查看“R.java”文件得知。

此处发现找不到R.java

2 android:layout_width 宽度

3 android:layout_height 高度

4 android:orientation 方向(垂直/水平)

 默认方向是水平分布

5 android:background 背景

 可以是颜色或者图片

6 android:padding 内边距

  • paddingLeft
  • paddingRight
  • paddingTop
  • paddingBottom

7 android:layout_margin外边距

  • layout_marginLeft
  • layout_marginRight
  • layout_marginTop
  • layout_marginBotton

8 View(注意V要大写)

是所有控件的父类,在控件上再插入控件

 <LinearLayout
        android:id="@+id/11_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#000000"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="50dp"
        android:paddingBottom="10dp"
        android:layout_marginBottom="20dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033"/>
    </LinearLayout>

9 android:layout_weight 权重

想要把一个控件用两种颜色平分

  <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1"/>

  <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#FF0033"
            android:layout_weight="1"/>

同理,也可多种颜色,不同权重分配

10 控件居中 android:layout_grivaty = "center"

练习代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/11_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#000000"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="50dp"
        android:paddingBottom="10dp"
        android:layout_marginBottom="20dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:background="#0066FF"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#FF0033"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>