通过代码创建android视图
我需要创建文本视图(或类似)dinamicaly,我是一个新手,我需要你的帮助。 这些是从Web服务接收的参数通过代码创建android视图,android,android-layout,Android,Android Layout,我需要创建文本视图(或类似)dinamicaly,我是一个新手,我需要你的帮助。 这些是从Web服务接收的参数 String jacuzzi=yes String estacionamiento = no String peli= yes String cable=yes String roomservice= no i need to create them when the (variable == "yes") 我一直在寻找答案,但我不明白 我不知道我是否
String jacuzzi=yes
String estacionamiento = no
String peli= yes
String cable=yes
String roomservice= no
i need to create them when the (variable == "yes")
我一直在寻找答案,但我不明白我不知道我是否真的需要创建一个新的布局,
我对安卓真的很陌生 这里我把我的android xml放在这里
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="#ccccfe" >
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="45dp" >
<ImageView
android:id="@+id/imageView0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ffd7bd"
android:orientation="vertical"
android:layout_marginLeft="-200dp "
android:textStyle="bold"
android:textColor="#000000" android:shadowColor="#8a6603"
android:shadowDx="3" android:shadowDy="2" android:shadowRadius="1.8"
android:src="@drawable/muestra2" />
</TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:text="Motel: "
android:textColor="#303f46"
android:textSize="21sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imagen"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:background="#ccccfe"
android:scaleType="fitXY"
android:padding="1dp"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView />
<TextView />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dirección: "
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp" />
<TextView
android:id="@+id/textView2"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737"/>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp"
android:text="Comuna: " />
<TextView
android:id="@+id/textView3"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp"
android:text="Descripcion: " />
<TextView
android:id="@+id/desc"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="Telefono: "
android:width="90dp" />
<TextView
android:id="@+id/telefono"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="Correo: "
android:width="90dp"/>
<TextView
android:id="@+id/correo"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="SitioWeb:"
android:width="90dp" />
<TextView
android:id="@+id/sitio"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
这是如何使用代码而不是XML动态创建布局 首先,您应该在xml中为LinearLayout提供一个id:
<LinearLayout android:id="@+id/myLayout">
但是,请避免使用字符串并将其设置为“是”。用布尔值代替 最好创建它们并在不需要时删除它们,而不是按如下所示的相反方式 首先
boolean jacuzzi = true;
boolean estacionamiento = false;
boolean peli = true;
boolean cable = true;
boolean roomservice = false;
使用布尔而不是字符串。布尔值可以是true或false。使用“是”或“否”更容易
//如果必须使用字符串
使用
而不是下面的布尔人
//
我将向你们展示按摩浴缸,所有人都使用相同的技术
TextView tv = (TextView) findViewById(R.id.textView1);
我叫它电视,但你可以叫它任何东西。至于R.id.textView1——这是指android自动调用textView1的XML代码中的TextView,但您可以通过更改id来更改它
if(jacuzzi == false)
{tv.setVisibility(View.GONE);}
这表示如果按摩浴缸为false,则使文本视图消失。你也可以使用
if(!jacuzzi)
{tv.setVisibility(View.GONE);}
因为他们的意思是一样的
否则,对于动态编码来说,要困难得多。您需要通过findViewById引用您的linearlayout,类似于我使用textview的方式。然后你需要创建一个新的文本视图。如果你觉得你真的想学习如何做这件事,那就更难了,不过还是问问吧 不幸的是,这些变量来自字符串形式的Web服务。。谢谢你的回答你最好还是用它们来设定布尔值。例如,使用if(jacuzzi.contentEquals(“yes”){bjacuzzi=true;}else{bjacuzzi=false;}别忘了点击勾号来接受答案。没问题,更新它来帮助你!感谢我的朋友给你的耐心,你应该是一名老师。chiley的问候试图实现你的代码,但没有在屏幕上显示任何内容。不客气。@user1676486谢谢你的编辑。我同意布尔值。
if(jacuzzi == false)
{tv.setVisibility(View.GONE);}
if(!jacuzzi)
{tv.setVisibility(View.GONE);}