安卓开发问题集锦
1. ShapeDrawable的资源文件的内边距问题
drawable
目录下的新建一个shape_bg.xml
,键入如下代码
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--设置一个黑色边框-->
<stroke
android:width="5dp"
android:color="#000000"/>
<!-- 渐变色 -->
<!-- gradient 梯度、坡度 -->
<!--
android:angle属性,值为0表示从左到右,值为90表示从下到上,值为45表示从左下到右上
值为135表示从右下到左上,值必须是45的倍数
-->
<gradient
android:angle="315"
android:startColor="#FCD209"
android:endColor="#C0C0C0"/>
<!-- 设置一下边距,让空间大一点 -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners
android:radius="15dp"/>
<!--<solid-->
<!-- android:color="#ffffff"/>-->
</shape>
绘制图形如下:
activity_main.xml
键入如下内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_bg"
android:text="你好,世界!"/>
</RelativeLayout>
</LinearLayout>
主页面布局如下:
调节shape_bg.xml
文件中padding
标签中的属性的值
<!-- 设置一下边距,让空间大一点 -->
<padding
android:left="50dp"
android:top="50dp"
android:right="50dp"
android:bottom="50dp"/>
主页面布局更改为如下:
2. Android Studio的Button
背景色都是紫色且无法修改
将themes.xml
文件
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<!--Theme.MaterialComponents.DayNight.NoActionBar.Bridge-->
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
修改为:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<!--Theme.MaterialComponents.DayNight.NoActionBar.Bridge-->
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
3. 获取单选框文本的两种方法
第一种:
package edu.tyut;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class RadioButtonActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobutton_test);
Button button = findViewById(R.id.btnPost);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
// 第一种方法
radioGroup.setOnCheckedChangeListener(((group, checkedId) -> {
RadioButton radioButton = findViewById(checkedId);
Toast.makeText(getApplicationContext(), "您选了: " + radioButton.getText(),
Toast.LENGTH_LONG).show();
}));
}
}
第二种:
package edu.tyut;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class RadioButtonActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobutton_test);
Button button = findViewById(R.id.btnPost);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
// 第二种方法
// 1:
button.setOnClickListener(view -> {
RadioButton radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(getApplicationContext(), "您提交了1: " + radioButton.getText(),
Toast.LENGTH_LONG).show();
});
// 2:
button.setOnClickListener(view -> {
for(int i = 0; i < radioGroup.getChildCount(); i ++ ){
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if(radioButton.isChecked()){
Toast.makeText(getApplicationContext(), "您提交了: " + radioButton.getText(),
Toast.LENGTH_LONG).show();
break;
}
}
});
}
}