Android界面开发基础(一)


一、基础控件

1、textview

文本框:
1、设置文本阴影:对常见的属性要记住。

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:textColor="@color/black"
        android:text="Hello Android Hello Android Hello Android Hello Android"
        android:textSize="30sp"
        android:background="@color/white"
        android:textStyle="italic"
        android:shadowColor="@color/purple_700"
        android:shadowRadius="3.0"
        android:shadowDx="10"
        android:shadowDy="10"/>

2、设置跑马灯:

主要是以下几个属性设置:
android:singleLine=“true”
android:ellipsize=“marquee”
android:focusable=“true”
android:focusableInTouchMode=“true”
android:marqueeRepeatLimit=“marquee_forever”
在这里插入图片描述

方式一:

设置clickable="true"属性,这种方式只有在点击获取焦点后才能达到跑马灯效果。

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!my baby Hello World!my babyHello World!my babyHello World!my baby"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:clickable="true"

在这里插入图片描述
一开始是不动的,只有获取焦点后才会动。

方式二:
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!my baby Hello World!my babyHello World!my babyHello World!my baby"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever" >
        <requestFocus/>
    </TextView>

属性 requestFocus是设置页面启动即可实现跑马灯的效果,不用点击获取焦点之后才能实现跑马灯的效果。

方式三:

自定义textview,在方法isFocused()中返回true,这样不用点击获取焦点就能


public class MyTextView extends androidx.appcompat.widget.AppCompatTextView {

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

同时要使用自定义的全路径名称。

 <com.example.textview_demo.MyTextView

方式二和方式三不需要点击获取焦点就能运行。
在这里插入图片描述

二、Button

1.StateListDrawble资源

通过该资源可以为按钮按下或者松开呈现不同的背景图:在drawble文件夹下创建btn_image.xml文件
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_baseline_accessibility_24"
        android:state_pressed="true">

    </item>
    <item android:drawable="@drawable/ic_baseline_account_balance_24"
        android:state_pressed="false"
        >

    </item>
</selector>

然后再布局文件中引用此资源:

<Button
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="button_press"
        android:textColor="#ff0000"
        android:textSize="20sp"
        android:background="@drawable/btn_image"

在这里插入图片描述
此时点击按钮和松开时会呈现不同图片。

2.点击事件

代码如下(示例):

 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("button","onClick");
            }
        });

        //长按事件
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.d("button","onLongClick");
                return false;
            }
        });

        //触摸事件
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.d("button","onTouch"+motionEvent.getAction());
                return true;
            }
        });

OnTouch方法会返回三个值:点击:返回0,松开:返回1,移动:返回2。如果在该方法中return true,那么onclick方法和onLongClick方法都不会执行。如图,只有OnTouch事件执行。
在这里插入图片描述
如果OnLongCLick方法return true那么onclick方法不会执行。
可以看出onclick方法没有执行。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值