android - 列表为空时添加按钮

标签 android android-layout

我试图在列表为空时动态添加按钮,即没有数据填充列表。我尝试了下面的代码,但它不起作用

public class TableDemoActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);

            Button test = new Button(this);
            test.setText("Hello Android");
            test.setId(5);
            test.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            linearLayout.addView(test);

        }

    }

这是布局文件内容

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TableLayout 
    android:id="@+id/TblLyt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <TableRow
        android:id="@+id/AcctHeader"
    >
    </TableRow>

    <ExpandableListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/BankExpandableListView"
        android:layout_width="fill_parent"
        android:layout_height="443dp"
        android:layout_weight="1.32"
    >
    </ExpandableListView>

</TableLayout>
</LinearLayout>

最佳答案

您可以将您的按钮放在 xml 布局文件中,并根据您的条件执行 visibleinvisible

if(your condition)
{
button.setVisibility(View.VISIBLE);
}
else
{
button.setVisibility(View.GONE);
}

关于android - 列表为空时添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732076/

相关文章:

Android经典的类似IRC的聊天框

android - 如何以编程方式滚动 recyclerView?

android - 如何在短信接收时从 android 检索 sim 详细信息

android - 如何在 Android 上设置行高 textView

android - 模拟器不给出错误,但设备在 putExtra() 上给出 java.lang.NullPointerException

java - 无法实例化以下类 :com. google.android.gms.ads.AdView(打开类,显示错误日志)

android - 如果 Android 应用程序崩溃,Realm 数据是否未加密?

android - 需要Gradle 1.8版;安装;必须使用Gradle 1.9版;安装;重复

android - 文本在 TextView 中看不清楚?

android - 由于setCotentView(R.layout_file_name)行的内存不足异常,导致应用程序崩溃