android默认布局色值,Android:从偏好设置中选择主题颜色

试试这个方法:

首先,在colors.xml中使用以下值定义新颜色:

#4FC3F7

#FDD835

然后,您定义一个像这样的新主题(my_theme.xml):

@color/my_blue

@color/my_blue

@color/my_blue

然后,看看我如何动态更改颜色:

我的布局:

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Blue"

android:id="@+id/btnBlue"

android:layout_below="@+id/imageView"

android:layout_alignLeft="@+id/imageView"

android:layout_alignStart="@+id/imageView"

android:background="#162c69" />

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Yellow"

android:id="@+id/btnYellow"

android:layout_below="@+id/imageView"

android:layout_alignRight="@+id/imageView"

android:layout_alignEnd="@+id/imageView"

android:background="#9b8a09" />

style="?android:attr/buttonStyleSmall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Mix"

android:id="@+id/btnMix"

android:layout_below="@+id/imageView"

android:layout_centerHorizontal="true"

android:background="#920f08" />

我的活动:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.setTheme(R.style.MyRandomTheme);

setContentView(R.layout.activity_main);

//Change theme to yellow

Button yellowButton = (Button) findViewById(R.id.btnYellow);

yellowButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (Build.VERSION.SDK_INT >= 21) {

getWindow().setNavigationBarColor(getResources().getColor(R.color.my_yellow));

getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow));

}

}

});

//Change theme to blue

Button blueButton = (Button) findViewById(R.id.btnBlue);

blueButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (Build.VERSION.SDK_INT >= 21) {

getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue));

getWindow().setStatusBarColor(getResources().getColor(R.color.my_blue));

}

}

});

//Change theme to mixture of blue and yellow

Button mixButton = (Button) findViewById(R.id.btnMix);

mixButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (Build.VERSION.SDK_INT >= 21) {

getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue));

getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow));

}

}

});

}

结果:

cd8b9780d74f3c5edc34cd79a6992847.png

另外,要从首选项中读取和写入首选项,请尝试以下操作:

//Write to preferences

String s = "this is a test.";

SharedPreferences prefs = this.getSharedPreferences("MyPrefs",this.MODE_PRIVATE);

SharedPreferences.Editor editor = prefs.edit();

editor.putString("A", s);

editor.apply();

//Fetch from preferences

SharedPreferences prefs2 = this.getSharedPreferences("MyPrefs", this.MODE_PRIVATE);

if(prefs2 != null) {

String text2 = prefs.getString("A","");

Log.d(LOG_TAG, "This is the string: "+text2);

}

您可以传递“ R.color.my_yellow”之类的参数,并将其设置为您的首选项,直到您的用户对其进行更改.希望对您有所帮助.如果您有任何问题,请告诉我.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值