2016-08-27 4 views
0

がCardViewインスタンスを作成し、LinearLayout.LayoutParamsを添付し、CardViewに2 TextViewsを追加しましたが、TextViewsではなく、あなたが望むものを達成するために別の2つのテキストビューをプログラム的に作成するには、縦に並べます。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT 
    ); 

    card.setLayoutParams(params); 
    int margin = 10; 
    params.setMargins(margin, margin, margin, margin); 
    // Set CardView corner radius 
    card.setRadius(15); 
    // Set cardView content padding 
    card.setContentPadding(20, 20, 20, 20); 
    // Set a background color for CardView 
    card.setCardBackgroundColor(Color.parseColor("#FFC6D6C3")); 
    // Set the CardView maximum elevation 
    card.setMaxCardElevation(15); 
    // Set CardView elevation 
    card.setCardElevation(9); 

    // Initialize a new TextView to put in CardView 
    TextView tv = new TextView(mContext); 
    tv.setText(title); 
    tv.setTypeface(null, Typeface.BOLD); 
    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17); 
    tv.setTextColor(Color.parseColor("#006699")); 


    // Put the TextView in CardView 
    card.addView(tv); 

    TextView tv1 = new TextView(mContext); 
    tv1.setText(title); 
    tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); 
    tv1.setTextColor(Color.parseColor("#006699")); 

    card.addView(tv1); 
    // Finally, add the CardView in root layout 
    rl.addView(card); 

答えて

1

以下のものを揃えるの互いの上に重なって、あなたが作成する必要がありますオブジェクトLinearLayoutにはTextViewが含まれ、CardViewオブジェクトにはLinearLayoutが追加されます。

関連する問題