Android开发学习笔记 1
1.布局管理器
1.线性布局(LinearLayout)
常用属性
- android:id 控件标识(布局本身也是一个控件)
- android:layout_width 布局宽度
- android:layout_height 布局高度
- android:background 布局背景(颜色、图片、xml文件等)
- android:layout_margin 外边距
- android:layout_padding 内边距
- 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
- 文字大小、颜色
<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>
- 显示不下用…
<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"/>
- 中划线、下划线
代码展示
<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)
- 文字大小颜色
<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"/>
- 自定义背景形状
//在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>
- 点击事件
<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();
}
}