android - 以编程方式创建 UI 时 appcompat-v7 :22. 0.0 主题问题

标签 android android-appcompat

更新到 appcompat-v7:22 后,我遇到了严重的主题问题。 appcompat-v7:21 不会出现下述问题

我有一个静态添加的 fragment 的 Activity 。 fragment View 仅以编程方式创建(不会扩展布局)。最终的 Activity View 包含 2 个按钮:一个直接在 Activity 布局中创建,第二个通过 Fragment 以编程方式添加。 第二个按钮应与第一个按钮类似,因为未分配自定义样式或属性值:

enter image description here

应用程序主题扩展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/

相关文章:

android - RecyclerView 的第一项丢失

Android 谷歌浏览器在锁屏应用程序后不启动

java sugaring,我可以在这里避免几乎重复的代码吗?

java - 带有 RecyclerView 和 CollapsingToolbarLayout 的 CoordinatorLayout

android - Widget.AppCompat.Button.Colored Button 禁用时透明

android - 在 Android 中以编程方式隐藏工具栏主页/后退按钮?

Android访问webview历史记录

Android - 视频重新启动或恢复

android.view.ActionMode vs android.support.v7.view.ActionMode,我应该使用哪一个,有什么区别?

android - 是什么取代了 AndroidX 中的 appcompat-v7