动态创建控件时如何访问它们(如果没有)。控制的数量未知
我创建了一个带有文本视图和 3 个按钮的复合控件。当我使用 xml 将其添加到布局时,它工作得很好。但需要在运行时使用java代码添加它。当我尝试使用 java 代码添加它时,其他控件可见,但我的复合控件未显示。
的复合控件 XML JAVA 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/border_lines"
>
<TextView android:id="@+id/msg_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAMPLE MESSAGE TITLE"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/btn_shw"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="SHOW MSG"
android:layout_weight="1"
/>
<Button android:id="@+id/btn_dis"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text=" DISABLE"
android:layout_weight="1"
/>
<Button android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text=" DELETE "
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
用于添加控件
package deepak.android.remainder;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class RemainderList extends Activity
{
ScrollView sv1;
LinearLayout ll1;
deepak.android.remainder.RemainderControl rc1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sv1=new ScrollView(this);
ll1=new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
sv1.addView(ll1);
TextView tv1=new TextView(this);
tv1.setText("THIS IS SAMPLE TEXT");
ll1.addView(tv1);
**LinearLayout.LayoutParams lp;
lp=new LineararLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);**
for(int i=0;i<5;i++)
{
rc1=new deepak.android.remainder.RemainderControl(this);
**ll1.addView(rc1,lp);**
}
setContentView(sv1);
}
}
I created a compound control with a textview and 3 buttons. It works fine when i add it to a layout using xml. But it need to add it using java code at runtime. When i try to add it using java code the other controls are visible but my compound control is not shown.
COmpound Control XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/border_lines"
>
<TextView android:id="@+id/msg_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAMPLE MESSAGE TITLE"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/btn_shw"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="SHOW MSG"
android:layout_weight="1"
/>
<Button android:id="@+id/btn_dis"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text=" DISABLE"
android:layout_weight="1"
/>
<Button android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text=" DELETE "
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
JAVA CODE USED TO ADD THE CONTROLS
package deepak.android.remainder;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class RemainderList extends Activity
{
ScrollView sv1;
LinearLayout ll1;
deepak.android.remainder.RemainderControl rc1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sv1=new ScrollView(this);
ll1=new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
sv1.addView(ll1);
TextView tv1=new TextView(this);
tv1.setText("THIS IS SAMPLE TEXT");
ll1.addView(tv1);
**LinearLayout.LayoutParams lp;
lp=new LineararLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);**
for(int i=0;i<5;i++)
{
rc1=new deepak.android.remainder.RemainderControl(this);
**ll1.addView(rc1,lp);**
}
setContentView(sv1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过查询
ll1
(LinearLayout
)的特定(编号)子级来完成。例如,当然,在转换和使用子级之前,您需要执行 null 检查和
instanceof
检查。You can do by querying for specific (numbered) children of
ll1
(theLinearLayout
). For example,Of course, you will need to do a null check and a
instanceof
check, before you can cast and use the children.