小编Mat*_*hbl的帖子

Android工具栏中心标题和自定义字体

我正在尝试找出使用自定义字体作为工具栏标题的正确方法,并将其置于工具栏中(客户端要求).

目前,我正在使用旧的ActionBar,我将标题设置为空值,并使用setCustomView自定义字体TextView并使用ActionBar.LayoutParams将其居中.

有没有更好的方法呢?使用新工具栏作为我的ActionBar.

android android-layout android-theme android-styles android-toolbar

347
推荐指数
16
解决办法
34万
查看次数

Android工作室 - Gradle - GitHub Repo

有没有人知道是否有可能使用github repo作为依赖关系,而不会将其发布到maven central.

假设我正在开发一个拥有它自己的github repo的android库.我希望能够在我的android工作室项目中"编译"这个库具有gradle依赖性,而不必发布到maven central(至少暂时).

换句话说:我想使用不在maven central上的依赖项.这是一个直接的github repo(一个也使用gradle的android库).

我想我的build.gradle做这样的事情:

dependencies {
  // Google Play Services (normal dependency)
  compile "com.google.android.gms:play-services:5.2.08"

  // The library I want to pull from github
  compile "path_to_my_github_repo"
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

android github gradle maven android-studio

8
推荐指数
1
解决办法
1623
查看次数

Android工具栏setNavigationIcon不起作用

所以,我有一个BaseActivity,我有一个工具栏,我调用setSupportActionBar(工具栏).

在我扩展BaseActivity的一些活动中,我想将导航图标(默认箭头)更改为另一个drawable.但是当我调用toolbar.setNavigationIcon(myDrawable)它不起作用时,它仍然显示默认的左箭头图标.

任何的想法?谢谢.

android android-toolbar

7
推荐指数
3
解决办法
7958
查看次数

在 Repository 和 ViewModel 之间共享相同的 MutableLiveData

我正在考虑架构组件/MVVM。

假设我有一个存储库、一个 ViewModel 和一个 Fragment。我使用一个Resource类作为包装器来公开网络状态,就像架构组件指南中所建议的那样。

我的存储库目前看起来像这样(为简洁起见进行了简化):

class MyRepository {

  fun getLists(organizationId: String) {
    var data = MutableLiveData<Resource<List<Something>>>()
    data.value = Resource.loading()

    ApolloClient().query(query)
        .enqueue(object : ApolloCall.Callback<Data>() {
            override fun onResponse(response: Response<Data>) {
                response.data()?.let {
                    data.postValue(Resource.success(it))
                }
            }

            override fun onFailure(exception: ApolloException) {
                data.postValue(Resource.exception(exception))
            }
        })
}
Run Code Online (Sandbox Code Playgroud)

然后在 ViewModel 中,我还声明了一个 MutableLiveData:

var myLiveData = MutableLiveData<Resource<List<Something>>>()

fun getLists(organizationId: String, forceRefresh: Boolean = false) {
   myLiveData = myRepository.getLists(organizationId)
}
Run Code Online (Sandbox Code Playgroud)

最后,片段:

viewModel.getLists.observe(this, Observer {
        it?.let {
            if (it.status.isLoading()) showLoading() else hideLoading()

            if …
Run Code Online (Sandbox Code Playgroud)

android mvvm kotlin android-livedata android-architecture-components

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

工具栏NavigationIcon松散主题

我根据我想要的actionBar颜色(深色或浅色)为我的应用程序使用两个主题: - Theme.AppCompat.Light.NoActionBar - Theme.AppCompat.NoActionBar

这是我的工具栏布局:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:maxLines="1"
        android:ellipsize="end"
        android:textAppearance="?android:attr/textAppearanceLarge"
        />

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

在我的清单中,我将Application主题设置为light,一些活动将属性主题设置为dark.

我看到我的工具栏有一种奇怪的行为.假设我正在使用白色工具栏进行活动(活动使用灯光主题),然后我导航到带有红色工具栏(具有黑暗主题)的活动.当我使用白色工具栏返回活动时,导航图标(左指向箭头)现在为白色.就像它采取了我的黑暗主题.

希望我能清楚地解释一下情况.

谢谢!

android android-toolbar

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

Android MVVM-如何使LiveData发出其拥有的数据(强制触发观察者)

我有ViewModel从网络获取列表的列表,并RecyclerView用数据填充了a (MyAvailabilityRepository返回a MutableLiveData,这就是我正在使用的原因Transformations.switchMap):

class MyAvailabilityViewModel : ViewModel() {
    private val getListsParams = MutableLiveData<String>()
    private val getListsObservable = Transformations.switchMap(getListsParams) { 
        organizationId -> MyAvailabilityRepository.getSectionedLists(organizationId) 
    }

    fun getListsObservable() : LiveData<Resource<MutableList<SectionedAvailabilityList>>> {
        return getListsObservable
    }

    fun fetchLists(organizationId: String, forceRefresh: Boolean = false) {
        if (getListsParams.value == null || forceRefresh) {
            getListsParams.value = organizationId
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

片段的onActivityCreated

override fun onActivityCreated(savedInstanceState: Bundle?) {
    ...
    viewModel.getListsObservable().observe(this, Observer { // populate RecyclerView })
    viewModel.fetchLists(organizationId)
}
Run Code Online (Sandbox Code Playgroud)

由于 …

android android-mvvm android-livedata android-architecture-components

6
推荐指数
2
解决办法
1183
查看次数

Retrofit 2.0 - 自定义 CallAdapterFactory - 主线程上未发生回调

我正在将 Android 应用程序迁移到 Retrofit 2.0。我有一个自定义的 ErrorHandler 扩展,RetrofitError这样我就可以对不同的 Http 错误做出反应。

现在我明白我必须创建一个自定义CallAdapterFactory. 我使用了这里ErrorHandlingCallAdapter提供的示例。

我的结果CallAdapter几乎是相同的代码,但如果需要,我也可以发布我的代码。

发生的情况是,当我使用 this 时CallAdapterFactory,主线程上没有发生回调。当我android.view.ViewRootImpl$CalledFromWrongThreadException尝试更新用户界面时(我总是需要这样做)。我也不想总是在回调中使用 runOnUIThread 包装我的代码。

我不知道这是否有帮助,但是当我登录Thread.currentThread().getName()回调时,它返回 OkHttp。

android retrofit

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