【问题标题】:Creating a CardView programmatically does not apply style correctly以编程方式创建 CardView 无法正确应用样式
【发布时间】:2015-07-29 12:28:29
【问题描述】:

我正在尝试从代码创建一个CardView。但是,它似乎没有正确应用样式。以下是样式:

<style name="CardViewStyle" parent="CardView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">8dp</item>
</style>

<style name="Widget.CardContent" parent="android:Widget">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:paddingLeft">16dp</item>
    <item name="android:paddingRight">16dp</item>
    <item name="android:paddingTop">24dp</item>
    <item name="android:paddingBottom">24dp</item>
    <item name="android:orientation">vertical</item>
</style>

在 XML 中,它看起来像这样:

<android.support.v7.widget.CardView
style="@style/CardViewStyle">
    <LinearLayout
        style="@style/Widget.CardContent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Caption"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

现在我正在尝试在 Java 中做同样的事情:

CardView card = new CardView(new ContextThemeWrapper(MyActivity.this, R.style.CardViewStyle), null, 0);
LinearLayout cardInner = new LinearLayout(new ContextThemeWrapper(MyActivity.this, R.style.Widget_CardContent));

TextView tv_title = new TextView(this);
tv_title.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
));
tv_title.setTextAppearance(this, R.style.TextAppearance_AppCompat_Title);
tv_title.setText("Name");

TextView tv_caption = new TextView(this);
tv_caption.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
));
tv_caption.setText("Sus");

cardInner.addView(tv_title);
cardInner.addView(tv_caption);

这是结果。在此图像中,第一个 CardView 是由 XML 创建的。第二个 CardView 是以编程方式创建的。似乎第二个仅适用于parent="CardView",因为其他属性(layout_width、layout_height、layout_margin)已正确应用。

【问题讨论】:

    标签: android android-styles android-cardview


    【解决方案1】:

    由于某种原因,设置边距会使整个事情再次起作用。

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    int margin = dpToPixel(8);
    params.setMargins(margin, margin, margin, margin);
    card.setLayoutParams(params);
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,这对我有帮助:CardView has lost margin when inflating

      我将 null 作为 CardView 的父级传递给 View.inflate(),这意味着 xml layout_marginLeft 等参数将被忽略。

      【讨论】:

      • 推荐类似的 SO 答案会更好作为评论。如果问题重复,则应将其中一个或另一个标记为关闭。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多