【问题标题】:AddView error, Just show the first elementAddView 错误,只显示第一个元素
【发布时间】:2014-02-22 13:02:45
【问题描述】:

假设我有一个 LinearLayout,我想向它添加两个 View。第一个包含editText,另一个包含listview。我一直在尝试java中的代码如下:

    EditText inputViaText;
    ListView historyInput;
    protected static LinearLayout askTextLayout = null;

    askTextLayout = new LinearLayout(this);
    askTextLayout.setVisibility(LinearLayout.VISIBLE);
    askTextLayout.setOrientation(LinearLayout.HORIZONTAL);

    inputViaText =  new EditText(this);
    LinearLayout.LayoutParams askTextParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    historyInput = new ListView(this);
    LinearLayout.LayoutParams historyInputParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,70);

    askTextLayout.addView(historyInput,historyInputParams);
    askTextLayout.addView(inputViaText,askTextParams);

    FrameLayout.LayoutParams frameAskTextParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
    setContentView(R.layout.activity_main);
    addContentView(askTextLayout, frameAskTextParams);  

但是,它只显示我添加的第一个。所以当我编写如下代码时:

     askTextLayout.addView(historyInput,historyInputParams);
     askTextLayout.addView(inputViaText,askTextParams);

它只显示列表视图。当我编写如下代码时:

     askTextLayout.addView(inputViaText,askTextParams);
     askTextLayout.addView(historyInput,historyInputParams);

它只显示编辑文本。谁能帮帮我?

【问题讨论】:

    标签: java android view add


    【解决方案1】:

    在您的活动的 oncreate 方法中尝试此代码

    context = this;
    
            setContentView(R.layout.activity_main);
            container = (LinearLayout) findViewById(R.id.Linear);
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setWeightSum(100);
            ListView v1 = new ListView(context);
            v1.setBackgroundColor(Color.CYAN);
            LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,
                    50);
            p1.weight = 90;
            v1.setLayoutParams(p1);
            EditText v2 = new EditText(context);
            v2.setText("Hello");
            v2.setBackgroundColor(Color.WHITE);
            LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,
                    50);
            p2.weight = 10;
            v2.setLayoutParams(p2);
            linearLayout.addView(v1, p1);
            linearLayout.addView(v2, p2);
            View view = new View(MainActivity.this);
    
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                    1);
            view.setLayoutParams(lp);
            view.setBackgroundColor(Color.BLACK);
            container.addView(linearLayout);
            container.addView(view);
    
    
    Write this in activity_main:
    
    <LinearLayout
            android:id="@+id/Linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </LinearLayout>
    

    【讨论】:

    • 这个线性 ID 是从哪里来的? "container = (LinearLayout) findViewById(R.id.Linear);"
    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多