亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以編程方式在RelativeLayout中部署視圖?

如何以編程方式在RelativeLayout中部署視圖?

慕哥9229398 2019-10-14 12:08:08
如何以編程方式在RelativeLayout中部署視圖?我試圖以編程方式(而不是通過XML聲明)實現以下目標:<RelativeLayout...>    <TextView ...      android:id="@+id/label1" />    <TextView ...      android:id="@+id/label2"       android:layout_below: "@id/label1" /></RelativeLayout>換句話說,我如何使第二個TextView出現在第一個代碼下面,但我想在代碼中這樣做:RelativeLayout layout = new RelativeLayout(this);TextView label1 = new TextView(this);TextView label2 = new TextView(this);... layout.addView(label1);layout.addView(label2);setContentView(layout);最新情況:謝謝,TreeUK。我理解總體方向,但仍然行不通-“B”與“A”重疊。我做錯什么了?RelativeLayout layout = new RelativeLayout(this);TextView tv1 = new TextView(this);tv1.setText("A");TextView tv2 = new TextView(this); tv2.setText("B");RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(         RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);         lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());layout.addView(tv1);                 layout.addView(tv2, lp);
查看完整描述

3 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

從我能夠拼湊起來的東西來看,你必須使用LayoutParams添加視圖。

LinearLayout linearLayout = new LinearLayout(this);RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        parentView.addView(linearLayout, relativeParams);

所有的功勞都歸功于sechaden,為了相對地以編程方式定位您的項目,您必須將ID分配給它們。

TextView tv1 = new TextView(this);tv1.setId(1);TextView tv2 = new TextView(this);tv2.setId(2);

然后addRule(RelativeLayout.RIGHT_OF, tv1.getId());



查看完整回答
反對 回復 2019-10-15
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

Android 22最小可運行示例

資料來源:

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;
import android.widget.RelativeLayout;import android.widget.TextView;public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final RelativeLayout relativeLayout = new RelativeLayout(this);

        final TextView tv1;
        tv1 = new TextView(this);
        tv1.setText("tv1");
        // Setting an ID is mandatory.
        tv1.setId(View.generateViewId());
        relativeLayout.addView(tv1);

        // tv2.
        final TextView tv2;
        tv2 = new TextView(this);
        tv2.setText("tv2");
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        lp.addRule(RelativeLayout.BELOW, tv1.getId());
        relativeLayout.addView(tv2, lp);

        // tv3.
        final TextView tv3;
        tv3 = new TextView(this);
        tv3.setText("tv3");
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT        );
        lp2.addRule(RelativeLayout.BELOW, tv2.getId());
        relativeLayout.addView(tv3, lp2);

        this.setContentView(relativeLayout);
    }}

生成的默認項目一起工作。android create project ...構建代碼最少的GitHub存儲庫.



查看完整回答
反對 回復 2019-10-15
  • 3 回答
  • 0 關注
  • 450 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號