简体   繁体   English

Android AnalogClock:以编程方式设置drawables

[英]Android AnalogClock : setting drawables programmatically

I'm making an analog clock app which has around 15 designs in one app. 我正在制作一个模拟时钟应用程序,在一个应用程序中有大约15个设计。 One way of setting the designs would be to create different widgetconfigs.XML for each design but that'll clutter up devices running 4.0+. 设置设计的一种方法是为每个设计创建不同的widgetconfigs.XML ,但这会使运行4.0+的设备widgetconfigs.XML混乱。

I also thought of setting up an activity which allows changing of the design using 我还想过设置一个允许更改设计的活动

remoteviews.setInt(R.id.analogClock1, "setDialResource", R.drawable.clock1);

But this won't allow the hand drawables to be set... What else can I do? 但这不允许设置手绘画......我还能做什么?

you actualy draw drawables once(when activity starts) of course there is some animations and alot of thing that change the look of activity but not drawables you can use drawable for different objects in activity like having one style for each button but you can not redraw. 你实际绘制drawables一次(当活动开始时)当然有一些动画和很多改变活动外观的东西,但不是drawables你可以使用drawable为活动中的不同对象,如每个按钮有一个样式,但你不能重绘。

you have 2 ways to do this project: 你有两种方法来做这个项目:

1.using XMLs 1.使用XML

you can make 15 diffrent activities and by Intent you can switch between activities 你可以做15个不同的活动,通过Intent你可以在活动之间切换

public void method()
{
    Intent i = new Intent(this , Destination.class);
    startActivity(i);
}

2.using codes 2.使用代码

anything that can be designed using XMLs also can be written via java codes. 任何可以使用XML设计的东西也可以通过java代码编写。 it takes more lines but it can be dynamic it means you don't have to make if else for each condition ; 它需要更多的线条,但它可以是动态的,这意味着你不必为每个条件制作if else; you simply produce needed objects each time it is needed . 您只需在每次需要时生成所需的对象。

examples: 例子:

Xml XML

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />

or programmatically: 或以编程方式:

Button button = new Button(this); YourView.addview(button);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM