安卓入门 --- 视图

安卓的入门

所有的部件都可以设置visibility属性:三个可选参数:
1.可以看见
2.不可见但占据空间
3.不可见且不占据空间

1.TextVIiew

  • 设置id可以找到文本框
  • layout_width 和layout_height有两个属性match_parent和wrap_content(还有一个已经弃用),match_parent与父级相同,wrap_content由文本的内容所决定。
  • text:是指文本框所要写的内容
  • gravity和laout_gravity的区别是前者是指文本内容在文本框的位置,后者是指这个视图在整个布局占的地方
  • 文字的大小单位是sp。
  • padding:内边距,magrin:外边距
  • layout_weight:占比,如果其他的视图没有设置的话,其他的视图安装设置的weight占比,然后再与其他设置了的进行比较
  • textAllCaps来设置大小写的问题
  • 跑马灯的效果: android:ellipsize=“marquee”
    android:singleLine=“true”
    和tv.setSelected(true)配合使用就可以,还可以设置循环的次数
 <TextView
        android:id="@+id/tofind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你好"
        android:background="@color/black"
        android:textColor="@color/white"
        android:gravity="center"
        android:padding="10dp"
        android:textSize="20sp"
        android:paddingTop="15dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:layout_margin="10dp"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:textAllCaps="true"
       

        />

运行结果:

在这里插入图片描述

2.Button.(继承自Textview)

设置三种Onclick
1.xml设置(在高版本中已经删)

   android:onClick="usefunction"
public void usefunction(View view) {
        editText.setText("成功了");
    }

2.java代码

 button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            }
        });

3.实现接口的办法(适用于比较多点击事件的情况)

public class MainActivity extends AppCompatActivity implements View.OnClickListener 

public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.big: Toast.makeText(MainActivity.this,"小的",Toast.LENGTH_SHORT).show();
                break;
            case R.id.small: Toast.makeText(MainActivity.this,"大的",Toast.LENGTH_SHORT).show();
                break;
            case R.id.one: Toast.makeText(MainActivity.this,"大da的",Toast.LENGTH_SHORT).show();
                break;
            case R.id.two: Toast.makeText(MainActivity.this,"大xiao的",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    
  button1.setOnClickListener(this);//每一个按钮只需要使用this指针便指向了当前的MainActivity

让Button改变颜色的方法:
<1.androidx.appcompat.widget.AppCompatButton
<2.切换主题:

3.EditText

  • hint表示的是提示的内容
  <EditText
            android:id="@+id/ed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_launcher_foreground"
            android:drawableRight="@drawable/ic_launcher_foreground"
            android:hint="要输入的内容"
            />

获取焦点的方式

        EditText editText  = findViewById(R.id.ed);
        editText.setText("你好");
       //可以无 String string = editText.getText().toString();
       //editText.setSelection(string.length)
        editText.requestFocus();

4.ImageView

<ImageView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_foreground"
        android:scaleType="centerCrop"
        这行是为了照片和设置的大小相适应
                />

5.复选框

  <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="nihao"
            />

使用结果:
在这里插入图片描述

6.选项
设置在同一个Group下面就会只能选择一个,可以设置默认被选择

<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nihao"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nihao"/>
    </RadioGroup>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值