我发现从 Android Studio 4.1 开始,我无法Button通过在 a 上设置颜色来更改 a 的背景颜色android:background,只是没有效果。自定义Drawable也不起作用。
我的背景Drawable:
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1.5dp"
        android:color="@android:color/black" />
    <solid
        android:color="@android:color/white" />
    <corners
        android:radius="8dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我的Button:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add To Cart"
    android:background="@drawable/background3"/>
Run Code Online (Sandbox Code Playgroud)
结果:
android android-button android-drawable android-styles material-design
我的实体类:
@Entity(tableName = "student")
data class Student(
    @PrimaryKey(autoGenerate = true)
    val id: Long,
    val name: String,
    val age: Int,
    val gpa: Double,
    val isSingle: Boolean
) 
Run Code Online (Sandbox Code Playgroud)
问题是,由于id是由Room数据库自动生成的- 意味着无论我id在构造函数中放入什么,它无论如何都会被覆盖,并且因为它是构造函数中的参数之一,我必须id每次都给出像这样:
val student = Student(0L, "Sam", 27, 3.5, true)
Run Code Online (Sandbox Code Playgroud)
我怎样才能避免编造id这样我就可以像这样输入必要的数据:
val student = Student("Sam", 27, 3.5, true)
Run Code Online (Sandbox Code Playgroud) 我注意到的唯一区别是,如果我在prepare()before调用play(),我会看到进程指示器,并且它会在 中预加载数据,此外,如果我只是在 withoutPlayerView调用,我无法分辨出区别。play()prepare()
此外,文档什么也没说:https ://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.html
public void prepare()
Description copied from interface: Player
Prepares the player.    //<- ???
Specified by:
prepare in interface Player
Run Code Online (Sandbox Code Playgroud) 所以我正在更新我的RecylerView内容StateFlow<List>如下:
我的数据类:
data class Student(val name: String, var isSelected: Boolean)
Run Code Online (Sandbox Code Playgroud)
我的视图模型逻辑:
fun updateStudentsOnSelectionChanged(targetStudent: Student) {
    val targetIndex = _students.value.indexOf(targetStudent)
    val isSelected = !targetStudent.isSelected
    _students.value[targetIndex].isSelected = isSelected        //<- doesn't work
} 
Run Code Online (Sandbox Code Playgroud)
问题: UI没变,但isSelected内部_student变了,这是怎么回事?(和...一样LiveData)
android kotlin android-livedata kotlin-flow kotlin-stateflow
我从 Vector Asset 中绘制的矢量:
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="@android:color/holo_purple"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M7,14l5,-5 5,5z" />
</vector>
Run Code Online (Sandbox Code Playgroud)
我的布局:
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_baseline_arrow_drop_up_24" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:text="Hi, my name is Sam"
        android:textColor="@android:color/white"
        android:textSize="30sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
电流输出:
如何删除这个矢量可绘制的内部填充?任何帮助将不胜感激。
xml android drawable android-drawable android-vectordrawable
我想知道coroutineScope工作完成后是否会自动取消。coroutineScope假设我在自定义类中创建一个而不是 ViewModel类或Fragment / Activity类:
class MyClass {
    private val backgroundScope = CoroutineScope(Dispatchers.Default)
    fun doSomething() = backgroundScope.launch {
        //do background work
    }
}
Run Code Online (Sandbox Code Playgroud)
那么,后台工作完成后,会backgroundScope自动取消吗?
android kotlin kotlin-coroutines android-threading coroutinescope
我尝试使用下面的代码创建一个弹出窗口,顶部有一个类似工具提示的箭头。附图片。但结果我得到了不同的东西。
弹出式充气机:
LayoutInflater inflater = (LayoutInflater)
                        getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View  view = inflater.inflate(R.layout.popup, null);
              mypopupWindow = new PopupWindow(view,500, RelativeLayout.LayoutParams.WRAP_CONTENT, true);            
              mypopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
              mypopupWindow.showAsDropDown(v,-153,0);
Run Code Online (Sandbox Code Playgroud)
弹出布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="50dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/shadow_recta"
        android:orientation="vertical"
        android:gravity="center">
     <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="text long text" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="top|center_horizontal" >
        <rotate android:fromDegrees="0" android:toDegrees="-45"
            android:pivotX="0%" android:pivotY="50%" >
            <shape android:shape="rectangle">
                <size android:width="24dp" android:height="24dp" />
                <stroke android:color="@android:color/holo_blue_bright" android:width="1dp"/>
            </shape>
        </rotate>
    </item>
    <item>
        <shape android:shape="rectangle"> …Run Code Online (Sandbox Code Playgroud) 刚刚了解到DataBinding并发现toString()Kotlin的强大内置功能不可用:
<layout 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="student"
            type="com.example.databindingtest2.Student" />
    </data>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{student.name}"
        android:textColor="@android:color/black"
        android:textSize="30sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@{student.age.toString()}"    //doesn't work, age is integer
        android:textColor="@android:color/black"
        android:textSize="30sp" />
</layout>
Run Code Online (Sandbox Code Playgroud)
我知道String.valueOf()会起作用,但这不是 Kotlin 的方式。任何帮助,将不胜感激。
android kotlin kotlin-extension android-databinding android-jetpack
只是测试Preferences DataStore并发现提供的Flow输出不会发出相同的值,我的设置如下:
数据存储实用程序类:
object DataStore {
    private val Context.settings by preferencesDataStore("settings") 
    suspend fun saveBoolean(context: Context, keyResId: Int, value: Boolean) {
        val key = booleanPreferencesKey(context.getString(keyResId))
        context.settings.edit {
            it[key] = value
        }
    }
    fun getBooleanFlow(context: Context, keyResId: Int, defaultValueResId: Int): Flow<Boolean> {
        val key = booleanPreferencesKey(context.getString(keyResId))
        val defaultValue = context.resources.getBoolean(defaultValueResId)
        return context.settings.data.map {
            it[key] ?: defaultValue
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
视图模型类:
class FirstViewModel(application: Application) : AndroidViewModel(application) {
    private val uiScope = viewModelScope
    val isUpdateAvailable = DataStore.getBooleanFlow(
        getApplication(), R.string.is_update_available_key, R.bool.is_update_available_default …Run Code Online (Sandbox Code Playgroud) android android-preferences sharedpreferences kotlin-flow android-jetpack-datastore
android ×10
kotlin ×4
kotlin-flow ×2
android-room ×1
android-xml ×1
drawable ×1
exoplayer ×1
exoplayer2.x ×1
xml ×1