更新到 appcompat-v7:22 后,我遇到了严重的主题问题。 appcompat-v7:21 不会出现下述问题
我有一个静态添加的 fragment 的 Activity 。 fragment View 仅以编程方式创建(不会扩展布局)。最终的 Activity View 包含 2 个按钮:一个直接在 Activity 布局中创建,第二个通过 Fragment 以编程方式添加。 第二个按钮应与第一个按钮类似,因为未分配自定义样式或属性值:
应用程序主题扩展Theme.AppCompat.NoActionBar
Activity 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:text="Super Button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.jskierbi.appcompat22test.MainFragment" />
</LinearLayout>
fragment 类:
public class MainFragment extends Fragment {
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(getActivity());
btn.setText("Click me!");
layout.addView(btn);
return layout;
}
}
我已将此问题分离到此处的新项目:https://github.com/jskierbi/appcompat-v7-22-bug
这是一个错误还是我遗漏了什么? 是否可以为此制定解决方法?
编辑
这不是一个错误。 <Button>
布局中定义的小部件会膨胀为 View 层次结构中的 TintButton 对象。可能的解决方法是在代码中创建 TintButton 而不是 Button。 警告 TintButton 位于内部包内,因此不应在生产代码中使用。
最佳答案
据我所知,v21 不支持使用 Material 指南自动设置按钮样式。我假设现在在运行时创建的按钮没有样式,是的,这可能被认为是一个错误。
想到的可能的解决方法(现在无法测试)是调用 new Button()
,new TintButton()
,其中 TintButton 是支持库中定义的类,假定为 Button
的 Material 样式版本。
我认为应该是android.support.v7.internal.widget.TintButton
。
关于android - 以编程方式创建 UI 时 appcompat-v7 :22. 0.0 主题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29323804/