我有这个 Kotlin 代码:
if(this.fragmentMeasurementBinding.viewStub.isInflated)
{
return;
}
this.fragmentMeasurementBinding.viewStub.viewStub?.layoutResource =
R.layout.layout_measurement_single_value
this.fragmentMeasurementBinding.viewStub.setOnInflateListener { _, inflated ->
LayoutMeasurementSingleValueBinding.bind(inflated)?.let {
it.textInputLayoutSingleMeasurementValue.hint = Helper.getDynamicStringResource(
this.context, parent.getItemAtPosition(position).toString(), prefix = "title_")
}
}
this.fragmentMeasurementBinding.viewStub.viewStub?.inflate()
Run Code Online (Sandbox Code Playgroud)
这在我的 XML 文件中:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MeasurementFragment">
<ViewStub
android:id="@+id/viewStub"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_input_layout_measure" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout_measure"
style="@style/AppTheme.Widget.TextInput.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:descendantFocusability="blocksDescendants"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<AutoCompleteTextView
android:id="@+id/exposed_dropdown_measure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/what_are_you_measure"
android:imeOptions="actionNext"
android:labelFor="@id/text_input_layout_measure" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我编译Android studio时报如下错误:
我正在使用来自 Google 的新 …
android kotlin android-studio android-gradle-plugin android-viewbinding
当我使用 ViewBinding 时,它总是生成xxxxxBinding.
我知道android使用Android Gradle Plugin带有代码和资源\xe3\x80\x82的构建apk
他们将分割一些任务(或转换)。
\n\n我知道任务(转换)会将一些文件构建到./build.
但是ViewBinding让我很困惑\xef\xbc\x8c他似乎没有生成文件\xef\xbc\x88因为每次创建布局文件时我都可以直接访问它\xef\xbc\x89(或者我可以直接删除build文件夹就不会再创建了\xef\xbc\x8c但是他还是可以正确的\xef\xbc\x88代码中没有错误\xef\xbc\x89)\xe3\x80\x82
\n\n是否有一些文件被秘密生成?
\n\n如果有的话,会在哪里生成呢?
\n\n如果没有,我该如何做同样的事情?例如,使用markdown文件实时生成代码。
\nandroid android-gradle-plugin android-databinding android-viewbinding
我无法获得带有合并根标记的布局来处理视图绑定。是否可以?
include_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/include_test"
layout="@layout/merge_layout"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
合并布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/include_test">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A Title"/>
</merge>
Run Code Online (Sandbox Code Playgroud)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = IncludeLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.includeTest.title.text = "New Title"
}
Run Code Online (Sandbox Code Playgroud)
运行时异常:
java.lang.NullPointerException: Missing required view with ID: includeTest
我想用 ViewBinding 创建一个片段,但我的代码不起作用。我阅读了ViewBinding 文档,但我的片段没有显示。这是我的代码:
片段播放器.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test fragment"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
布局_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_player"
class="com.test.plo.view.PlayerFragment"
android:name="com.test.plo.view.PlayerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
PlayerFragment.java
public class PlayerFragment extends Fragment {
private FragmentPlayerBinding fragmentPlayerBinding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
fragmentPlayerBinding = FragmentPlayerBinding.inflate(inflater, container, false);
View view = fragmentPlayerBinding.getRoot();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
fragmentPlayerBinding = null;
}
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
碰巧我有一个主应用程序模块
构建.gradle
dynamicFeatures = [":myFeature"]
viewBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
AdroidManifest.xml
package="com.mydomain.testproject"
Run Code Online (Sandbox Code Playgroud)
使用我在整个应用程序 eq 中使用的一些常见布局: app/res/error_view.xml
我有动态特征文件
构建.gradle
viewBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
AdroidManifest.xml
package="com.mydomain.testproject.myFeature"
Run Code Online (Sandbox Code Playgroud)
myfeature_fragment.xml
<include
android:id="@+id/error_view"
layout="@layout/error_view"
android:visibility="gone" />
Run Code Online (Sandbox Code Playgroud)
我的特征片段.kt
binding = MyFeatureFragmentBinding.bind(view)
Run Code Online (Sandbox Code Playgroud)
问题来了。当我尝试访问binding.errorViewAS 时显示错误Cannot access class 'com.mydomain.testproject.myFeature.databinding.ErrorViewBinding'. Check your module classpath for missing or conflicting dependencies
奇怪的是,当我检查生成的MyFeatureFragmentBinding类时,它包含以下内容
@NonNull
public final View errorView;
Run Code Online (Sandbox Code Playgroud)
所以我想 AS 知道的比它显示给我的要多。我还发现在主模块的生成类中,原始ErrorViewBinding和 OFC 从那里可以完美地工作。
有人设法从另一个功能模块引用通用布局?
或者如何强制生成的公共视图绑定的类型?
因为我通过启用发现了视图绑定的使用
buildFeatures {
viewBinding true
}
Run Code Online (Sandbox Code Playgroud)
在我的 gradle 文件中,我再也没有在代码中使用 findviewById 。我现在想知道这样做是否有缺点。如果这是最好的方法,为什么android studio在创建新项目时默认不启用此选项?如果不是,我什么时候应该避免使用视图绑定?谢谢。
我想开始viewBinding在我们的项目中使用,但仅仅添加配置就会导致编译错误:
android {
buildFeatures {
dataBinding true
viewBinding true // new line and only change
}
Run Code Online (Sandbox Code Playgroud)
结果是:
e: /home/leo/StudioProjects/android-wallet/mbw/build/generated/source/kapt/btctestnetDebug/com/mycelium/wallet/DataBinderMapperImpl.java:37: error: cannot find symbol
import com.mycelium.wallet.databinding.FragmentBequantAccountBindingImpl;
^
symbol: class FragmentBequantAccountBindingImpl
location: package com.mycelium.wallet.databinding
Cannot find a setter for <com.mycelium.wallet.databinding.ItemBequantSearchBinding app:visibility> that accepts parameter type 'int'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
Run Code Online (Sandbox Code Playgroud)
有问题的代码是:
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="com.mycelium.bequant.market.viewmodel.AccountViewModel" />
</data>
...
<include …Run Code Online (Sandbox Code Playgroud) 我尝试在 Android 项目中使用 ViewBinding,因为 Kotlin 合成已被弃用。我遵循 Android 开发者网站的官方文档。在我的 build.gradle.kts 中,我启用了 ViewBinding:
android {
...
buildFeatures {
viewBinding = true
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在片段中使用它,例如:
private var _binding: FragmentStartBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentStartBinding.inflate(inflater, container, false)
return binding.root
}
Run Code Online (Sandbox Code Playgroud)
但binding.root显示错误:Cannot access class 'android.widget.no_name_in_PSI_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40'. Check your module classpath for missing or conflicting dependencies。
Android Studio 版本:4.1.2
Kotlin 版本:1.4.30
Android Gradle 插件版本:4.1.2
Gradle …
我正在更新一个应用程序,我读到现在处理视图的推荐方法是使用视图绑定。我按照说明进行操作,但是遇到了一些问题:
添加点击侦听器的效果如下:
((LinearLayout) findViewById(R.id.btn_login)).setOnClickListener(v -> {
Log.v(TAG, "findViewById press");
});
Run Code Online (Sandbox Code Playgroud)
而以下内容则不然
binding.btn_login.setOnClickListener(v -> {
Log.v(TAG, "View Binding press");
});
Run Code Online (Sandbox Code Playgroud)
文档说应该。我想在一个新项目上尝试这个,以确保它在某种程度上与应用程序配置无关,但我得到了相同的结果 - 它不起作用。
我像这样初始化它:
public class Login extends BaseClassFragmentActivity {
ActivityLoginBinding binding;
private final String TAG = "[LOGIN]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
binding = ActivityLoginBinding.inflate(getLayoutInflater());
}
}
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
如何在 SupportMapFragment 中使用视图绑定?参考我的代码
我在布局中有一个地图片段,我想在片段中使用视图绑定。
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
LZ需要解决方案..
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.zeddigital.zigmaster.R
import com.zeddigital.zigmaster.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private lateinit var mMap: GoogleMap
private var binding : FragmentHomeBinding ?=null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentHomeBinding.inflate(inflater)
// binding.myTextView.text = "sample"
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync { googleMap ->
}
return binding!!.root
}
override fun …Run Code Online (Sandbox Code Playgroud)