久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何以編程方式將 Android TextView 對齊到另一個

How to programmatically align an Android TextView to the right of another TextView(如何以編程方式將 Android TextView 對齊到另一個 TextView 的右側(cè))
本文介紹了如何以編程方式將 Android TextView 對齊到另一個 TextView 的右側(cè)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在下面的代碼中,我創(chuàng)建了兩個文本視圖,并以編程方式將它們添加到相對布局中.我想將它們并排對齊.

In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.

代碼運行良好,但沒有將新的 TextView 放在前一個 TextView 的右側(cè),而是將新的 TextView 放置在邊距(0,0,0,0) 即屏幕右上角:

The code runs fine but is not placing the new TextView to the right of previous TextView instead the new TextView is positioned at margin (0,0,0,0) i.e. upper right corner of the screen:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);

    textView[0] = new TextView(this);//creates first textview
    textView[0].setId(0);
    textView[0].setText("1");
    textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml

    relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    textView[0].setLayoutParams(relativeLayoutParams);

    relativeLayout.addView(textView[0]);//creates another textview

    textView[1] = new TextView(this);

    textView[1].setBackgroundResource(R.drawable.shape);

    RelativeLayout.LayoutParams relativeLayoutParams=
            new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview

    relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
    textView[1].setText("2");

    relativeLayout.addView(textView[1], relativeLayoutParams);

推薦答案

試試以下:

  1. 將textView[0]的id設(shè)置為1而不是0(id需要為正整數(shù))
  2. 在 textView[1] 的 relativeLayoutParams 中添加 RelativeLayout.ALIGN_TOP 的規(guī)則

以下內(nèi)容對我有用:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;       
TextView[] textView = new TextView[2];

// 1st TextView
textView[0] = new TextView(this);

relativeLayoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);

textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");   

relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

relativeLayout.addView(textView[0], relativeLayoutParams);

// 2nd TextView
textView[1] = new TextView(this);

relativeLayoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);      

textView[1].setText("2");

relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
        textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
        textView[0].getId()); // added top alignment rule

relativeLayout.addView(textView[1], relativeLayoutParams);

這篇關(guān)于如何以編程方式將 Android TextView 對齊到另一個 TextView 的右側(cè)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 日韩中文字幕一区二区 | 9久9久9久女女女九九九一九 | 成人三级av| 成人免费在线观看 | 激情网五月天 | 亚洲综合三区 | 欧美日韩在线一区二区三区 | 欧美在线a| 国产高清视频一区 | 欧美在线综合 | 成人免费看黄网站在线观看 | 亚洲精品久久久久久久久久久 | 亚洲第一成人影院 | 黄色网址在线免费观看 | 成人午夜免费在线视频 | 一本一道久久a久久精品综合 | 国产一区二区 | 中文字幕亚洲精品 | 国产福利久久 | 久久精品亚洲欧美日韩精品中文字幕 | 在线男人天堂 | 亚洲免费在线观看av | 在线免费小视频 | 成人乱人乱一区二区三区软件 | 久草在线 | 99热在这里只有精品 | 99re6在线视频精品免费 | 久久青| av在线一区二区三区 | 一区二区三区av | 成人小视频在线观看 | 日本超碰在线 | 成年人视频在线免费观看 | 亚洲97 | 一区二区视频在线 | 欧美中文字幕一区二区三区亚洲 | 久久婷婷国产麻豆91 | 日韩成人免费av | 欧美综合国产精品久久丁香 | 99久久久久| 51ⅴ精品国产91久久久久久 |