AndroidStudio之控件基础知识控件的⼤⼩,⼀般情况下直接⽤包裹内容(wrap_content)就可以了
如果要指定⼀定的⼤⼩,控件⼤⼩的单位,⼀般都是⽤dp为单位
Barrier        界线
Guideline    辅助线
多个控件对齐⽅式(左对齐,右对齐,顶对齐,底对齐等等)
(0dp)match_constraint  内容填充(填充到边界)
Alt + Enter ;  ⾃动导⼊缺少的类;
1. TextView 和 EditText 控件常⽤属性
android:layout_width="match_parent"        宽度与⽗控件⼀样宽
android:layout_height="wrap_content"      包裹内容,控件⼤⼩随着内容的⼤⼩⽽改变android:layout_marginStart="8dp"          左外边距8dp
android:layout_marginEnd="8dp"            右外边距8dp
android:background="#FFFFFF"              背景⾊
android:ems="10"
android:gravity="top"                      ⽂字对齐
android:hint="说点什么吧..."
android:inputType="textMultiLine"          多⾏输⼊
android:lines="6"                          显⽰⾏数
android:padding="5dp"                      内边距
android:drawableStart="@drawable/addpicture"  在编辑框放⼀个图⽚
2.Button 的 onClick 事件
另⼀个办法,在代码中创建按钮的事件
Button button = findViewById(R.id.button);
//给按钮创建[事件]
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"单击了按钮",Toast.LENGTH_SHORT).show();            }
});
3.按钮上设置 xml 背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="schemas.android/apk/res/android"
android:shape="rectangle">
<solid android:color="#1FBAF3"></solid>
<corners android:radius="5dp"></corners>
<padding
android:bottom="10dp"
android:left="15dp"
android:right="15dp"
android:top="10dp"
></padding>
</shape>
View Code
按钮属性加上: android:background="@layout/shape"
4.图⽚按钮
android:background="#0000"  背景透明
5.Activity 的切换
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();  //关闭Activity,返回到打开它的窗⼝,如果是启动窗⼝调⽤finish(),则直接退出程序
onCreate(null); //刷新窗⼝
直接修改启动窗⼝,在清单⽂件中修改,将 Main2Activity 修改成其他Activity,就会从其他窗⼝先启动,⼊⼝ Activity
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
6.Activity 之间数据传递 (Bundle  Intent)
窗⼝1代码:
Button button = findViewById(R.id.buttonBunldIntent);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = ((EditText)(findViewById(R.id.editTextUserName))).getText().toString().trim();
String pwd = ((EditText)findViewById(R.id.editTextPasword)).getText().toString().trim();
String mobile = ((EditText)findViewById(R.id.editTextMobile)).getText().toString().trim();
if(name=="" || pwd=="" || mobile =="" ){
Toast.makeText(BundleIntentActivity.this,"⽤户名、密码、⼿机号三者输⼊不完整!",Toast.LENGTH_SHORT);                    return;
}
Intent intent = new Intent(BundleIntentActivity.this,BundleIntentActivity2.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("name",name);
bundle.putString("pwd",pwd);
bundle.putString("mobile",mobile);
intent.putExtras(bundle);
startActivity(intent);
}
});
窗⼝2代码:
Intent intent = getIntent();
Bundle bundle = Extras();
String name = String("name");
String mobile = String("mobile");
String pwd = String("pwd");
TextView  tv = findViewById(ViewInfo);
tv.setText("name=" + name + "\n" + "pwd=" + pwd + "\nmobile=" + mobile);
6.Activity 返回数据
界⾯2,通过gridView 显⽰头像
android radiogroup
第1个界⾯,通过 startActivityForResult(intent) 跳到第2个界⾯,通过事件onActivityResult接收返回的数据
第2个界⾯,setResult(0x11,intent) 返回数据
界⾯1代码:
protected void onCreate(Bundle savedInstanceState) {
//选择头像
findViewById(R.id.buttonSelHead).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SelHeadActivity.class);
startActivityForResult(intent,0x11);
}
});
}
/
/返回数据显⽰到图⽚控件上
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == 0x11 && resultCode == 0x11){
Bundle bundle = Extras();
int imageId = Int("imageId");
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(imageId);
}
}
界⾯2代码:
ample;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class SelHeadActivity extends AppCompatActivity {
//定义头像数组
public int[] imageId = new int[]{1,2,3,4,5,6,7,8};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_sel_head);
GridView gridView = findViewById(idViewHead);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
//获取屏幕宽度
//获取窗⼝管理器
WindowManager wm = getWindowManager();
int width = wm.getDefaultDisplay().getWidth();
int ImageWidth = width/4-10;
if(convertView == null){
imageView = new ImageView(SelHeadActivity.this);
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(ImageWidth);
imageView.setMaxWidth(ImageWidth);
imageView.setPadding(5,5,5,5);
}
else{
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageId[position]);
return imageView;
}
};
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Intent intent = getIntent();
Bundle bundle = new Bundle();
bundle.putInt("imageId",imageId[position]); //实例化要传递的数据包
intent.putExtras(bundle);        //将数据包保存到 intent 中
setResult(0x11,intent); //设置返回的结果码
finish();  //关闭当前Activity
}
});
}
}
图⽚放在gridView 控件中
7. Dialog 提⽰框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("回答错误,下⾯请看解析:当张⼭");
builder.setPositiveButton("确定",null ).show();
8. android:layout_margin 与 padding
android:layout_margin 外边距
padding 内边距
9.设置成横屏显⽰
10.ImageView
adjustViewBounds 保持宽⾼⽐
hint 图⽚着⾊
scaleType 缩放类型
11. ImageSwitcher 图⽚切换器
ample;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
public class ImageSwitchTest2 extends AppCompatActivity {
private int[] arrayPictures={1,2,3,4,5,6,7,8};    private ImageSwitcher imageSwitcher;
private int index;
private float touchDownX;
private float touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_image_switch_test2);
//设置全屏
Window window = getWindow();
WindowManager.LayoutParams winParams = Attributes();
winParams.flags=WindowManager.LayoutParams.FLAG_FULLSCREEN;
window.setFlags(winParams.flags,winParams.flags);
imageSwitcher = findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(ImageSwitchTest2.this);
imageView.setImageResource(arrayPictures[index]);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。