相关疑难解决方法(0)

如何在Android中使用SharedPreferences来存储,获取和编辑值

我想存储一个时间值,需要检索和编辑它.我SharedPreferences该怎么用呢?

android sharedpreferences

599
推荐指数
20
解决办法
62万
查看次数

以编程方式获取颜色值(主题)

考虑一下:

styles.xml

<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="theme_color">@color/theme_color_blue</item>
</style>
Run Code Online (Sandbox Code Playgroud)

attrs.xml

<attr name="theme_color" format="reference" />
Run Code Online (Sandbox Code Playgroud)

color.xml

<color name="theme_color_blue">#ff0071d3</color>
Run Code Online (Sandbox Code Playgroud)

所以主题颜色由主题引用.如何以编程方式获取theme_color(引用)?通常我会使用getResources().getColor()但不是在这种情况下,因为它被引用!

android android-theme android-resources

93
推荐指数
6
解决办法
5万
查看次数

Android - 在屏幕上移动ImageView(像拖动一样)

我正在尝试创建一个可以ImageView在您的设备上移动的应用程序,例如拖动,当我放置75%ImageView的屏幕外显示Toast例如.我一直在阅读有关MotionEventonTouchListener我已经遵循了这一问题,但它并没有说服我.

编辑

我目前的代码是:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    int windowwidth;
    int windowheight;
    private ImageView mImageView;
    private ViewGroup mRrootLayout;
    private int _xDelta;
    private int _yDelta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        windowwidth = displaymetrics.widthPixels;
        windowheight = displaymetrics.heightPixels;
        mRrootLayout = (ViewGroup) findViewById(R.id.root);
        mImageView = (ImageView) mRrootLayout.findViewById(R.id.im_move_zoom_rotate);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
        mImageView.setLayoutParams(layoutParams);
        mImageView.setOnTouchListener(this);
    }
    public boolean onTouch(View view, …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout android-view

13
推荐指数
1
解决办法
7044
查看次数

动态更改colors.xml的值以更改android应用程序的外观

问题: 我正试图在运行中改变我的Android应用程序的外观和感觉.类似的东西,应用程序启动并从服务器获取一组值.这些值是通常在colors.xml内部的元素.我正在寻找的是一种动态更改colors.xml内部元素的方法,并使用从服务器接收的这些新值更新它.我的理解是,通常情况下,这不能直接完成.但有没有人找到解决方法?

如果可能的话我想避免: 我想避免在每个活动的onCreate()方法中为该视图中的每个元素设置颜色值.如果可能的话,我想避免这种情况.

有什么想法吗?

android android-layout

7
推荐指数
1
解决办法
5889
查看次数

以编程方式更改R.String的值

你能在android程序中以编程方式更改R.string的值吗?我需要提取一些API信息(例如电池状态,电池百分比,android os版本),并希望将其保存为R.string值.我知道怎么读它:

 String helloValue= getResources().getString(R.string.hello);
Run Code Online (Sandbox Code Playgroud)

我也看过:以编程方式更改R.string的值?但似乎只涉及改变语言,他最终以不同的方式做到了.有人可以伸出援助之手吗?我也看过这里:http://developer.android.com/guide/topics/resources/string-resource.html并找不到任何帮助:(

android android-2.3-gingerbread

6
推荐指数
1
解决办法
3万
查看次数

RecyclerView添加EmptyView

我正试图在我身上实施EmptyView,RecyclerView Adapter但我没有得到任何结果.

我已经按照本教程和这个提示,但没有人为我工作.

我实施了:

if (viewType == EMPTY_VIEW) {
    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
    EmptyViewHolder evh = new EmptyViewHolder(v);
    return evh;
}

v = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
Run Code Online (Sandbox Code Playgroud)

但它不允许我编译,因为它们是不同的ViewHolder,因为我创建了两个ViewHolder类,但他们extends Recycler.ViewHolder所以我没有得到它...

我正在尝试这样做,因为我有一个SearchView,我想要当列表为空它显示一个EmptyView,我已经得到它以编程方式做它但我更喜欢添加像一个layout因为我不知道那么多怎么样放置TextViews并以Buttons编程方式.

如果我放

return dataList.size() > 0 ? dataList.size() : 1;
Run Code Online (Sandbox Code Playgroud)

它给了我错误,因为索引是0.

我已经调试了viewType并且始终是1,那么它将不会加入if条件......

深入Android我发现了这个:

/** …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

6
推荐指数
1
解决办法
1625
查看次数

将ImageView拖放到容器中以进行验证

为了更好地理解它,请阅读以下内容:

First game :

Skate QuestionMark
Archery QuestionMark
Swim QuestionMark

---------------------------
Water Bow Wheel

If user drags Water to Skate or Archery QuestionMark it will animate to the list (because it is not correct)

If user drags twice incorrect (it will mark the one that is correct from the answer list)

If user still fail in the third try it will drag to the incorrect one and then it will highlight or just change the color doesn't matter to red. …
Run Code Online (Sandbox Code Playgroud)

java android drag-and-drop android-layout kotlin

5
推荐指数
1
解决办法
341
查看次数

是否可以引用styles.xml文件中的属性?

我想让用户可以切换整个应用程序的颜色皮肤.我的意思是当用户按下屏幕按钮时动态切换应用程序的某些自定义视图的样式.我知道如果你Activity.setTheme()onCreate()方法之前调用,你可以动态地改变应用程序的主题,但是NavigationView在xml布局上应用了自定义样式的普通视图(例如),没有setTheme或setStyle方法,所以它不会出现可以动态改变他们的风格.

我认为我的目标可能是引用在styles.xml文件中声明的AppTheme中声明的颜色.我的意思是,我可以宣布两个AppThemes,每个都有一组颜色,然后,在为自定义视图声明的自定义样式中,引用颜色.像这样的东西:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="customColor">#111111</item>
    </style>

    <style name="AppTheme.AnotherColor" parent="Theme.AppCompat">
        <item name="customColor">#222222</item>
    </style>

    <style name="CustomActionBar">
        <!-- title text color -->
        <item name="android:textColorPrimary">@styles/customColor</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

因此,默认情况下,我的"AppTheme"上声明的自定义Color将默认使用颜色应用111111.但是,当我使用应用setTheme(R.styles.AppTheme_AnotherColor)的颜色更改我的应用程序的主题时222222.如果这是可能的,那将是完美的!但它不可能或者我不知道如何直接从同一styles.xml文件的另一种样式访问样式中声明的颜色.我的意思@styles/customColor是不正确,我不知道如何访问这种颜色.

怎么能实现这一目标?

android android-layout android-theme android-styles

2
推荐指数
1
解决办法
2192
查看次数