我在我的CardView中添加了5个元素(ImageView,TextView和3个按钮)。我正在尝试设置RelativeLayout.LayoutParams并在addRule()中添加对齐,但不知何故没有得到预期的输出。
这是我的代码:
package bhadra.com.mca;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Dashboard extends AppCompatActivity {
Button button_add, button_reports, button_appointment, button_message;
Context context;
CardView cardview;
ImageView imageview;
RelativeLayout.LayoutParams layoutparams, textparams, imageparams,
buttonparams, buttonparams2, buttonparams3;
TextView textview;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
button_add = (Button)findViewById(R.id.btn_add);
context = getApplicationContext();
linearLayout = (LinearLayout)findViewById(R.id.layout_linear);
//cardview = (CardView)findViewById(R.id.card_patientdetails);
button_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createCardView();
}
});
}
public void createCardView() {
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,160 , getResources().getDisplayMetrics());
int margintop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,30 , getResources().getDisplayMetrics());
int imglayout = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60 , getResources().getDisplayMetrics());
int txtmargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,70 , getResources().getDisplayMetrics());
int drawpad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5 , getResources().getDisplayMetrics());
int cardpad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5 , getResources().getDisplayMetrics());
int cardpad2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10 , getResources().getDisplayMetrics());
int radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4 , getResources().getDisplayMetrics());
int elevation = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10 , getResources().getDisplayMetrics());
cardview = new CardView(context);
layoutparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, height);
layoutparams.setMargins(0,margintop,0,0);
imageparams = new RelativeLayout.LayoutParams(
imglayout, imglayout);
imageparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textparams.setMargins(0,txtmargin,0,0);
textparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonparams3 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonparams3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonparams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageview = new ImageView(context);
imageview.setLayoutParams(imageparams);
imageview.setImageResource(R.drawable.ic_account_circle_black_24px);
textview = new TextView(context);
textview.setLayoutParams(textparams);
textview.setText(R.string.patient_name);
textview.setTextSize(20);
textview.setTextColor(Color.BLACK);
button_reports = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_reports.setLayoutParams(buttonparams);
button_reports.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_assessment_black_24px, 0, 0, 0);
button_reports.setCompoundDrawablePadding(drawpad);
button_reports.setText(R.string.upload_reports);
button_reports.setTextSize(12);
button_appointment = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_appointment.setLayoutParams(buttonparams2);
button_appointment.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_today_black_24px, 0, 0, 0);
button_appointment.setCompoundDrawablePadding(drawpad);
button_appointment.setText(R.string.book_appointment);
button_appointment.setTextSize(12);
button_message = new Button(new ContextThemeWrapper(this, R.style.Widget_AppCompat_Button_Borderless), null, 0);
button_message.setLayoutParams(buttonparams3);
button_message.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_message_black_24px, 0, 0, 0);
button_message.setCompoundDrawablePadding(drawpad);
button_message.setText(R.string.message);
button_message.setTextSize(12);
cardview.setLayoutParams(layoutparams);
cardview.setRadius(radius);
cardview.setPadding(cardpad,cardpad2,cardpad, 0);
cardview.setMaxCardElevation(elevation);
cardview.addView(imageview);
cardview.addView(textview);
cardview.addView(button_reports);
cardview.addView(button_appointment);
cardview.addView(button_message);
linearLayout.addView(cardview);
}
}
此代码给出了以下输出: Output
有人可以帮帮我吗?