相关疑难解决方法(0)

Android:以编程方式向布局添加按钮

我正在尝试获取一个添加按钮,根据按钮左侧的edittext添加另一个按钮到布局.关键是一个人列出他们家中的房间,然后当他们在每个房间输入时,会生成一个新按钮,这样他们就可以点击房间,然后开始处理下一页.

我有一个XML布局全部完成,然后我意识到我"编程"添加按钮,所以我编程重做布局,然后在开关/箱(这就是我该怎么办onclicks)的添加按钮,我试图添加视图的按钮,但它变得非常棘手.我想在edittext下面有一个滚动视图并添加按钮,当他们将所有房间添加到他们的房子时,它最终会填充整个家庭的可滚动按钮列表.有没有办法以编程方式将按钮添加到xml'd布局.我以为你可以,但我正在尝试的一切都没有用.

感谢大家的帮助,非常感谢您的任何建议.

First Edit(响应Tanuj的解决方案)

我的XML文件(不确定我们是否会使用它或只是使用java):

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

<TextView
    android:id="@+id/tvAddARoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tvAddARoom" />

<EditText
    android:id="@+id/etAddARoom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/etAddARoom" />


<Button
    android:id="@+id/btnAddARoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnAdd" />

<TextView
    android:id="@+id/tvSelectARoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tvSelectARoom" />

<TextView
    android:id="@+id/tvNoRooms"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tvNoRooms" />

<Button
    android:id="@+id/btnViewAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnViewAll" />

 </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和Java.这一点都不正确,因为在java中我创建整个布局而不是使用上面的布局.只是不确定我是否可以弥合这两者.

package com.bluej.movingbuddy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
//import android.widget.ScrollView;
import android.widget.TextView;

public class EstimatorByRoom extends Activity …
Run Code Online (Sandbox Code Playgroud)

android android-layout

13
推荐指数
2
解决办法
6万
查看次数

动态地将视图添加到活动布局

我有一个自定义视图(TextView的扩展),我想动态添加到我的布局(不想将它包含在main.xml文件中).

本书说我在我的java代码中使用findViewById()获取RelativeLayout然后创建我的自定义视图的新实例,然后在RelativeLayout上使用addView添加新视图.

我没有收到任何错误,但当我点击我的按钮添加新视图时,没有任何事情发生(视图没有被添加).我是否需要在自定义视图(例如布局宽度,布局高度)上设置其他属性才能显示?

编辑:添加代码

// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Run Code Online (Sandbox Code Playgroud)

user-interface android textview relativelayout

7
推荐指数
1
解决办法
2万
查看次数