Android开发学习笔记 1

1.布局管理器

1.线性布局(LinearLayout)

常用属性

  1. android:id 控件标识(布局本身也是一个控件)
  2. android:layout_width 布局宽度
  3. android:layout_height 布局高度
  4. android:background 布局背景(颜色、图片、xml文件等)
  5. android:layout_margin 外边距
  6. android:layout_padding 内边距
  7. android:orientation 方向(vertical、horizontal)

注意LinearLayout默认是水平布局(horizontal)

代码举例

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:background="@color/colorPrimary"
            android:layout_marginTop="10dp">

        <View
                android:layout_width="0dp"
                android:layout_weight="1" //权重
                android:layout_height="match_parent"
                android:background="@color/colorBlack"
                ></View>

        <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                ></View>
        
    </LinearLayout>

效果图
在这里插入图片描述

2.相对布局 (RelativeLayout)

最常用属性

  • 与线性布局相同的属性
    1.android:id 控件标识(布局本身也是一个控件)
    2. android:layout_width 布局宽度
    3. android:layout_height 布局高度
    4. android:background 布局背景(颜色、图片、xml文件等)
    5. android:layout_margin 外边距
    6. android:layout_padding 内边距
  • 特有的属性
    1. android:layout_toLeftOf 在…的左边
    2. android:layout_toRightOf 在…的右边
    3. android:layout_alignBottom 跟…底部对齐
    4. android:alignParentBottom 跟父控件底部对齐
    5. android:layout_layout_below 在…的下面

代码举例

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <View
            android:id="@+id/view_1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/colorBlack"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>

    <View
            android:id="@+id/view_2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/colorPrimary"
            android:layout_toLeftOf="@id/view_1"
            android:layout_above="@id/view_1"
            />

</RelativeLayout>

效果图
在这里插入图片描述

2.组件

1).TextView

  1. 文字大小、颜色
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".TextViewActivity">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        android:layout_margin="10dp"/>
</LinearLayout>
  1. 显示不下用…
<TextView
        android:id="@+id/tv_2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        android:layout_margin="10dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:layout_marginTop="10dp"/>
  1. 中划线、下划线
    代码展示
<TextView
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello android!!!"
        android:textSize="24sp"
        android:textColor="@color/colorPrimary"
        android:layout_marginTop="10dp"/>

    <TextView
            android:id="@+id/tv_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello android!!!"
            android:textSize="24sp"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="10dp"/>
public class TextViewActivity extends AppCompatActivity {

    //声明控件
    private TextView mTv3;
    private TextView mTv4

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        //找到控件
        mTv3 = findViewById(R.id.tv_3);
        mTv4 = findViewById(R.id.tv_4);
        //添加中划线
        mTv3.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        //去除锯齿
        mTv3.getPaint().setAntiAlias(true);

        //添加下划线
        mTv4.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }
}

效果图
在这里插入图片描述
4. 跑马灯
代码展示

    <TextView
            android:id="@+id/tv_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello android!!!hello android!!!hello android!!!hello android!!!hello android!!!"
            android:textSize="24sp"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="10dp"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            />
public class TextViewActivity extends AppCompatActivity {

    //声明控件
    private TextView mTv5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        //找到控件
        mTv5 = findViewById(R.id.tv_5);
        //实现跑马灯
        mTv5.setSelected(true);
    }
}

2).Button(继承自textview)

  1. 文字大小颜色
<Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="20sp"
        android:textColor="@color/colorWhite"
        android:background="@color/colorBlack"/>

  1. 自定义背景形状
//在drawable下面定义新的xml文件bg_btn3.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

    <stroke
        android:width="1dp"
        android:color="@color/colorPrimary"/>

    <corners
        android:radius="5dp"/>

</shape>

<Button
            android:id="@+id/btn_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮3"
            android:textSize="20sp"
            android:textColor="@color/colorPrimary"
            android:background="@drawable/bg_btn3"
            android:layout_below="@id/btn_2"
            android:layout_marginTop="10dp"/>

效果在这里插入图片描述
3. 自定义按压效果

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

     //state_pressed通过这个属性进行定义
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/colorPrimary"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="@color/colorWhite"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
</selector>
  1. 点击事件
<Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        android:textColor="@color/colorPrimary"
        android:background="@drawable/bg_btn3"
        android:layout_below="@id/btn_2"
        android:layout_marginTop="10dp"/>
<Button
            android:id="@+id/btn_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="按钮4"
            android:textSize="20sp"
            android:textColor="@color/colorPrimary"
            android:background="@drawable/bg_btn4"
            android:layout_below="@id/btn_3"
            android:layout_marginTop="10dp"
            android:onClick="showToast"
            />
public class ButtonActivity extends AppCompatActivity {


    private Button mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        mBtn3 = findViewById(R.id.btn_3);
        //方法二
        mBtn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ButtonActivity.this,"btn3被点击了",Toast.LENGTH_SHORT).show();
            }
        });

    }
    
    //方法一,设置按钮点击事件
    public void showToast(View view){
        Toast.makeText(this, "btn4被点击了", Toast.LENGTH_SHORT).show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值