linwoain的个人blog

知我者谓我心忧,不知我者谓我何求

0%

最新方法直接看底部,最新版2.4版本最简单方式

as在2.1版本原生支持java8,只需要在build.gradle一些简单的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
android {
defaultConfig {
//省略其他配置
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

阅读全文 »

tips:必须使用24.0.0及以后的版本。

  1. 添加v7依赖(以添加的查看版本是否为>=24.0.0):

    1
    compile 'com.android.support:appcompat-v7:24.2.0'
  2. 修改样式:

    1
    2
    3
    4
    5
    6
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    </style>

    使用的样式中必须包含DayNight。

  3. 在activity中调用(必须继承自AppCompatActivity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MainActivity extends AppCompatActivity {
static boolean isNight;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void change(View view) {
LLogUtils.i(isNight);
if (!isNight) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
LLogUtils.log();
} else {
LLogUtils.log();
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
isNight = !isNight;
recreate();
}
}
  1. 修改夜间样式

    res目录下创建values-night,在其中放入对应的资源文件

注意,调用切换方法后,必须使用recreate()方法重建view。同时Activity以前的状态会丢失。注意保存数据。

原理:根据activity获取资源的方法getResources(),通过查找其源码,根据其原理,通过生成一个AssetManager并添加一个APK文件的 ,最终生成一个包含所有资源的Resource对象。由于 AssetManager无法直接实例化,所以通过反射方法获取其实例,并调用其添加apk包的addAssetPath()方法。使用DexClassLoader加载目标apk中的资源R类获取需要的资源id的值。使用Resource的get方法(getString(int id)、getDrawable(int id))即可获取到相应的资源。
源码如下:
a.生成一个包含apk路径的AssetManager实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static AssetManager getAssetManager(String filePath) throws            
ClassNotFoundException {
AssetManager assetManager = null;
Class&lt;?&gt; aClass = Class.forName("android.content.res.AssetManager");
Method[] methods = aClass.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("addAssetPath")) {
try {
assetManager = (AssetManager) aClass.newInstance();
method.invoke(assetManager, filePath);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return assetManager;
}
阅读全文 »

1
2
3
4
5
6
7
8
9
<style name="tab_bottom">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">5dp</item>

</style>

在低版本上出现了

只靠右,无法居中的问题,即时设置了gravity,最终找到解决方法,添加一句:

1
<item name="android:background">@null</item>

1
2
3
4
5
6
7
8
9
10
11
12
13
ListPopupWindow popupWindow = new ListPopupWindow(MainActivity.this);
popupWindow.setAdapter(new LinAdapter<String>(MainActivity.this,Arrays.asList("1","2","1","2","1","2","1","2","1","2")) {
@Override
protected View LgetView(int position, View convertView, ViewGroup parent) {
TextView text = (TextView) LViewHelper.getView(android.R.layout.simple_list_item_1, context);
text.setText(beans.get(position));
return text;
}
});
popupWindow.setWidth(200);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnchorView(view);
popupWindow.show();

此控件在android原生api与v7包中均存在。