我正在写一个课程项目,我需要创建一个自定义材料卡。但是当我继承一张材质卡时,样式就消失了。我在网上找到的所有解决办法都试过了。任何帮助我都将不胜感激。
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Case aCase = new Case();
aCase.setId(183928);
aCase.setStatus("Status");
aCase.setDescription("fermentum dui faucibus in ornare quam viverra orci sagittis eu");
CaseView caseView = findViewById(R.id.caseView);
caseView.setCase(aCase);
}
}
活动\u main.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=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="secondary_text"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="supporting_text"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
style="?attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="action_1" />
<com.google.android.material.button.MaterialButton
style="?attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="action_2" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.barmatograf.interpolith.view.CaseView
android:id="@+id/caseView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
截图https://i.stack.imgur.com/htrq0.png
自定义卡的模型类
package com.barmatograf.interpolith.model;
public class Case {
private Integer id;
private String description;
private String status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
案例视图.java
package com.barmatograf.interpolith.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.barmatograf.interpolith.R;
import com.barmatograf.interpolith.model.Case;
import com.google.android.material.card.MaterialCardView;
public class CaseView extends MaterialCardView {
private Case aCase;
private TextView number;
private TextView status;
private TextView description;
public CaseView(Context context) {
this(context, null);
}
public CaseView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.materialCardViewStyle);
}
public CaseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.case_view_layout, this);
number = findViewById(R.id.case_number);
status = findViewById(R.id.case_status);
description = findViewById(R.id.case_description);
}
public void setCase(Case aCase) {
this.aCase = aCase;
if (aCase != null) {
if (aCase.getId() != null)
number.setText(String.valueOf(aCase.getId()));
if (aCase.getStatus() != null)
status.setText(aCase.getStatus());
if (aCase.getDescription() != null) {
String descriptionText = aCase.getDescription();
int length = Math.min(descriptionText.length(), 100);
descriptionText = descriptionText.substring(0, length);
descriptionText = descriptionText.trim();
descriptionText = descriptionText + "...";
description.setText(descriptionText);
}
}
}
public Case getCase() {
return aCase;
}
}
案例\视图\布局.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:id="@+id/case_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6" />
</LinearLayout>
<TextView
android:id="@+id/case_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<TextView
android:id="@+id/case_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
样式
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="materialCardViewStyle">@style/Widget.MaterialComponents.CardView</item>
</style>
1条答案
按热度按时间bsxbgnwa1#
请注意,您的
CaseView
是一个MaterialCardView
(extends
),但你还有另一个MaterialCardView
作为膨胀xml的根。这是另一个View
无样式集(无xml)style
事实上CaseView
包含一个子-另一个子MaterialCardView
.我认为它应该尊重“全球”环境
AppTheme
,但这双鞋还是值得修的-CardView
case-使用<merge tag而不是rootMaterialCardView
在xml中。inflate
方法将添加所有放置在<merge>
标记到CaseView