簡體   English   中英

具有ImageView和TextView的RelativeLayout-將ImageView下方的TextView對齊,均以編程方式在RelativeLayout中居中

[英]RelativeLayout with ImageView and TextView - Align TextView below ImageView, both center in RelativeLayout Programatically

我有一個帶有ImageView和TextView的RelativeLayout。 我想將TextView放在ImageView(帶有少量填充)的正下方,並將ImageView和TextView都對齊到RelativeLayout的中心。

RelativeLayout以編程方式添加

我已經閱讀了一些有關對齊的問題,但是,我無法讓它們為我工作。 下面是我當前的代碼...

RelativeLayout的代碼

RelativeLayout relativeLayout = new RelativeLayout(this);

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);

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);

如果我為圖像和文本都設置了RelativeLayout.CENTER_IN_PARENT ,它們彼此重疊,這是可以理解的,因為RelativeLayout支持視圖的重疊。

我認為為textview設置RelativeLayout.BELOW將使其在圖像下方對齊,但事實並非如此。 我什至嘗試了RelativeLayout.ALIGN_BOTTOM進行textview,但是即使這樣也沒有用。

嘗試這個..

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);

要么

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);

以下過程也可能為您服務。 1)在垂直LinearLayout中添加圖像視圖和文本視圖。 2)將線性布局添加到相對布局的中心。(使用CENTER_IN_PARENT)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM