安卓笔记-加油-奥利给!

1、安卓中的单位

px: 手机屏幕的最小显示单位,与设备的显示屏有关

dp: 它是与设备无关的显示单位,与屏幕尺寸有关

sp: 它是专门设置字体的大小,在系统设置中可以调整字体大小。

2、视图的宽高

通过属性android:layout_width表达宽度,layout_height表示高度,主要的类型有下面三种:

match_parent:表示与上级视图保持一致

wrap_content: 表示与内容自适应

以dp为单位的具体尺寸

3、视图的对齐方式

采用layout_gravity属性,它指定了当前视图相对于上级视图的对齐方式。

采用gravity属性 指定了下级视图相对于当前视图的对齐方式

\他们的值都包括left,top,right,bottom,center,可以left|top表示即靠左也考上

4、线性布局

参数 android:orientation="vertical" 垂直布局 android:orientation="horizontal" 水平布局

线性布局子视图可以使用 android:layout_weight="1" 比重来设置所在宽度等比,

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:background="#ccc"

    android:orientation="vertical"
    >
    <!--放对应的视图组件-->
</LinearLayout>

5、相对布局

layout_centerInParent 相对于上级元素在什么位置

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="我在中间"
        android:layout_centerInParent="true"
        ></TextView>


</RelativeLayout>

6、网格布局

类似于html中的table,  关键属性

android:columnCount="2" 2行
 android:rowCount="2"     2列

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    >

</GridLayout>

7、滚动视图 scrollView

① ScrollView 垂直方向的滚动视图,layout_width属性值必须设置match_parent, layout_height=wrap_content.

② HorizontalScrollView 水平方向的滚动视图,水平方向滚动时,layout_width属性值为wrap_content, layout_height属性值设置为match_parent。

8、给按钮加监听

① 继承点击事件

② 绑定事件

③ 在点击响应方法加判断 

//继承VIew的点击监听事件  implements View.OnClickListener
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

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

        //绑定事件
        findViewById(R.id.button2).setOnClickListener(this);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        //判断是那个按钮点击的
        if (v.getId() == R.id.button2) {
            TextView tv =  findViewById(R.id.tv);
            tv.setText("哈哈 我点击了一下5555");
            int d = Log.d("aaaa", "5555");
        }
    }

    public void doclick(View view) {
        TextView tv =  findViewById(R.id.tv);
        tv.setText("哈哈 我点击了一下222");
        int d = Log.d("aaaa", "22222");
    }
}

9、图片显示

图片放置在res/drawable目录下,不能用纯数字格式。

还可以用ImageButton,图片按钮。

<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/aa"
        ></ImageView>
ImageView iv = findViewById(R.id.image);
iv.setImageResource(R.drawable.bb);

图片的显示类型

图片+文字处理方式

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/dd"
    android:text="下载"></Button>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值