我有一個可在多個活動中多次使用的卡布局。以編程方式添加自定義佈局
我custom_card.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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:background="#000000">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="10dp"
tools:padding="15dp">
</android.support.v7.widget.CardView>
</RelativeLayout>
我想包括這一個活動,所以我寫了一個卡類,並添加方法來改變文本和其他視圖值。
CustomCard.java
public class CustomCard extends RelativeLayout{
private Context mContext;
private LayoutInflater layoutInflater;
private View mView;
private ImageView mCardImg;
private TextView mCardTitle;
private TextView mCardDescription;
public CustomCard(Context context){
super(context);
mContext = context;
init();
}
public void init(){
layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_card, this, true);
}
// setters of texts and other view elements
}
我與任何活動中渲染此卡掙扎。我在活動內部創建了customCard對象,是否還需要編寫其他內容?
你動態地創建這個CustomCard或使用XML添加到您的活動? –
我動態創建使用佈局的CustomCard對象 –