使用ImageView和TextView的RelativeLayout - 在ImageView下面对齐TextView,两者都以编程方式居中于RelativeLayout

Vam*_*lla 2 android android-layout

我有一个带有ImageView和TextView的RelativeLayout.我想要ImageView下面的TextView(带有一个小填充)以及ImageView和TextView,它们在RelativeLayout中居中对齐.

RelativeLayout以编程方式添加

我已经阅读了有关对齐的一些问题,但是,我无法让它为我工作.以下是我目前的代码......

RelativeLayout的代码

RelativeLayout relativeLayout = new RelativeLayout(this);
Run Code Online (Sandbox Code Playgroud)

ImageView的代码

        ImageView image = new ImageView(this);  
        RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);

        //Setting the parameters on the Image
        image.setLayoutParams(lpImage);

        //adding imageview to relative layout
        relativeLayout.addView(image);
Run Code Online (Sandbox Code Playgroud)

TextView的代码

TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lpTextView.addRule(RelativeLayout.BELOW, image.getId());

            //Setting the parameters on the TextView
            textview.setLayoutParams(lpTextView);

            //Adding TextView to relative layout
            relativeLayout.addView(textview);
Run Code Online (Sandbox Code Playgroud)

如果我同时设置RelativeLayout.CENTER_IN_PARENT图像和文本,它们会相互重叠,这是可以理解的,因为RelativeLayout支持视图重叠.

我认为设置RelativeLayout.BELOWtextview会使它在图像下面对齐,但事实并非如此.我甚至尝试RelativeLayout.ALIGN_BOTTOM过textview,但即使这样也行不通.

Har*_*ran 5

试试这个..

RelativeLayout relativeLayout = new RelativeLayout(this);

RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);

ImageView image = new ImageView(this);  
    RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    //Setting the parameters on the Image
    image.setLayoutParams(lpImage);

    //adding imageview to relative layout
    relativeLayout.addView(image);

    TextView textview = new TextView(this);
    RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lpTextView.addRule(RelativeLayout.BELOW, image.getId());

        //Setting the parameters on the TextView
        textview.setLayoutParams(lpTextView);

        //Adding TextView to relative layout
        relativeLayout.addView(textview);
Run Code Online (Sandbox Code Playgroud)

要么

LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);

ImageView image = new ImageView(this);  
        LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //Setting the parameters on the Image
        image.setLayoutParams(lpImage);

        //adding imageview to relative layout
        linearLayout.addView(image);

TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            //Setting the parameters on the TextView
            textview.setLayoutParams(lpTextView);

            //Adding TextView to relative layout
            linearLayout.addView(textview);
Run Code Online (Sandbox Code Playgroud)