<item name="android:layout_height">match_parent</item>
控件高度占据父元素全部的剩余高度
<item name="android:layout_weight">1</item>
将父控件的剩余空间,按照设置的权重比例,进行再分配。
比如:
<LinearLayout
android:orientation="vertical">
<Button
android:layout_height="wrap_content"/>
<TextView
android:layout_height="0dp"
android:layout_weight=1/>
<Button
android:layout_height="wrap_content"/>
</LinearLayout>
剩余空间:父元素总高度
- 第一个Button的高度(wrap_content
) - 0dp
- 第二个Button的高度(wrap_content
)
layout_weight属性默认为0。所以 三个子控件的 layout_weight 属性之和是 1。
对于第一个 Button, 其所占高度: wrap_content + 0/1*剩余空间
对于TextView, 其所占高度: 0dp + 1/1*剩余空间
对于第二个 Button, 其所占高度: wrap_content + 0/1*剩余空间
由上,总体效果就是:两个Button的高度都是 wrap_content,TextView占据了剩余的全部高度。
android:background="@color/purple_500"
Button属性不加 backgroud属性的话,控件内部会有间距。
在写这行代码的时候,报了一个错:Variable expected
。
猜想可能原因,在做 ++运算的时候,其被操作数得是一个可寻址的变量,而不是中间结果(不可寻址的,如常量7,5,2等)。
类比于 C++中的左值和右值。
int countOrigin = ++ Integer.parseInt((String) tvCount.getText());
// 改进
int countOrigin = Integer.parseInt((String) tvCount.getText());
final int op = ++ countOrigin;
androidx.constraintlayout.widget.ConstraintLayout
app:layout_constraintStart_toStartOf="parent" // 控件的左侧 贴着 父元素的左侧
app:layout_constraintTop_toTopOf="parent" // 控件的顶部 贴着 父元素的顶部
注意只有这样:
[Start / End] to [Start / End]:左右
[Top / Bottom] to [Top / Bottom]:上下
是有效的。
app:layout_constraintStart_toTopOf="parent" // Start 对 Top 是无效的, 因为一个方向是左右, 一个方向是上下。
注意,在约束布局中,如果只有这样,去给控件设置边距的话,是无效的。
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
必须要有约束,否则设置的margin不会生效
app:layout_constraintTop_toXXXOf="xxxx"
app:layout_constraintStart_toXXXOf="xxxx"
这个东西是设置为第一个展示的,加了这个的话,activity 标签要设置属性 android:exported
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Intent-filter的四个属性Action,Category,Extras,Data
设置一些监听事件
Button btnShowToast = findViewById(R.id.btnShowToast);
btnShowToast.setOnClickListener(
// 弹出气泡
(view) -> Toast.makeText(MainActivity.this,"Hello World", Toast.LENGTH_SHORT)
.show()
);
final TextView tvCount = findViewById(R.id.tvCount);
Button btnCount = findViewById(R.id.btnCount);
btnCount.setOnClickListener(
(view) -> {
int countOrigin = Integer.parseInt((String) tvCount.getText());
tvCount.setText(Integer.toString(++ countOrigin));
}
);
发送方
public static final String MESSAGE = "名字";
Button btnSend = findViewById(R.id.send_message);
EditText et = findViewById(R.id.message);
btnSend.setOnClickListener(
(view) -> {
String s = et.getText().toString();
// 发送消息, 页面跳转
Intent intent = new Intent(MessageActivity.this,
OtherActivity.class);
intent.putExtra(MESSAGE,s);
startActivity(intent); // 页面跳转
}
);
接收方
TextView tvMessage = findViewById(R.id.tvCount);
Intent intent = getIntent(); // 接收传过来的消息
String message = intent.getStringExtra(MessageActivity.MESSAGE);
if(message != null){
if(tvMessage != null){
tvMessage.setText(message);
}
}
android 虚拟机显示在了界面里面,怎么把模拟器放到到android studio窗口外
onSaveInstanceState / onRestoreInstanceState
屏幕旋转时数据丢失问题
模拟器进行屏幕旋转,要打开这个才有效,不然进行旋转是无效的。
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
need to use a Theme.AppCompat theme (or descendant) with this Activity
使用android.support.v4.widget.SwipeRefreshLayout爆红:
1.使用androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2.配置 ‘androidx.swiperefreshlayout:swiperefreshlayout:1.0.0’ 插件
安卓报错android.support.v4.widget.SwipeRefreshLayout
修改项目名称和包名方法:
打开项目根目录下的setting.gradle
文件
Android Studio修改项目名称和包名方法
cannot resolve symbol ‘R’:
实在不行,清理下缓存,重启试试。
cannot resolve symbol ‘R’