无法将图像设置为android gridview.setEmptyView
我已经编写了一个无法将图像设置为android gridview.setEmptyView,android,Android,我已经编写了一个网格视图,运行良好。 当gridview没有项目时,我需要一个图像。 我试着像这样添加它 gridView.setEmptyView((ImageView)findViewById(R.drawable.empty)); empty.png是res/drawable中的图像 但是当网格视图为空时,图像视图不会显示 完整代码 oncreate setContentView(R.layout.main_activity); GridView gridView
网格视图
,运行良好。
当gridview
没有项目时,我需要一个图像。
我试着像这样添加它
gridView.setEmptyView((ImageView)findViewById(R.drawable.empty));
empty.png是res/drawable中的图像
但是当网格视图
为空时,图像视图
不会显示
完整代码
oncreate
setContentView(R.layout.main_activity);
GridView gridView = (GridView) findViewById(R.id.listhabbit_ma);
gridView.setEmptyView((ImageView)findViewById(R.drawable.empty));
habbitListAdapter = new HabbitListAdapter(AppManager.getHabbits());
gridView.setAdapter(habbitListAdapter);
habbitListAdapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, HabbitDetail.class);
intent.putExtra("position", position);
startActivityForResult(intent, habbit_delete);
你做得不对<代码>图像!=<代码>图像视图。例如,正确的方法如下(非常简化):
您还可以在
XML
布局中声明它,如@vipalpattel所示您做得不对<代码>图像!=<代码>图像视图。例如,正确的方法如下(非常简化):
您还可以在
XML
布局中声明它,如@vipalpattel所示您做得不对<代码>图像!=<代码>图像视图。例如,正确的方法如下(非常简化):
您还可以在
XML
布局中声明它,如@vipalpattel所示您做得不对<代码>图像!=<代码>图像视图。例如,正确的方法如下(非常简化):
您还可以在
XML
布局中声明它,如@vipalpattel所示以下布局和代码工作正常:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<TextView
android:id="@+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
以下布局和代码可以正常工作:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<TextView
android:id="@+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
以下布局和代码可以正常工作:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<TextView
android:id="@+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
以下布局和代码可以正常工作:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/listhabbit_ma"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:paddingBottom="3dp"
android:paddingTop="3dp">
</GridView>
<TextView
android:id="@+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
findViewById(R.drawable.empty)
将返回null
我尝试按照您的建议添加图像。。但是整个sceen并没有添加图像,而是变成了蓝色。。。只有按钮可见您发布的main.xml
版面没有按钮,请仔细阅读Android中的版面系统。我发布了完整的版面。该视图还应附加到树状视图,请查看我的编辑findViewById(R.drawable.empty)
将返回null
我尝试按照您的建议添加图像。。但是整个sceen并没有添加图像,而是变成了蓝色。。。只有按钮可见您发布的main.xml
版面没有按钮,请仔细阅读Android中的版面系统。我发布了完整的版面。该视图还应附加到树状视图,请查看我的编辑findViewById(R.drawable.empty)
将返回null
我尝试按照您的建议添加图像。。但是整个sceen并没有添加图像,而是变成了蓝色。。。只有按钮可见您发布的main.xml
版面没有按钮,请仔细阅读Android中的版面系统。我发布了完整的版面。该视图还应附加到树状视图,请查看我的编辑findViewById(R.drawable.empty)
将返回null
我尝试按照您的建议添加图像。。但是整个sceen并没有添加图像,而是变成了蓝色。。。只有按钮可见您发布的main.xml
版面没有按钮,请仔细阅读Android中的版面系统。我已经发布了完整的版面。该视图还应附加到树状视图,请查看我的edit@shivgarg对当然,如果我们谈论的是活动
我也做过同样的事情,你可以简单地写这个
。。ImageView im=新的ImageView(本);im.setImageResource(R.drawable.empty);gridView.setEmptyView(im);但并没有设置图像,整个屏幕变成蓝色,线性布局变得古怪。我知道这样问是不合逻辑的,但我无法调试it@shivgarg是的,如果我们谈论的是活动,当然你可以简单地写这个
。。ImageView im=新的ImageView(本);im.setImageResource(R.drawable.empty);gridView.setEmptyView(im);但并没有设置图像,整个屏幕变成蓝色,线性布局变得古怪。我知道这样问是不合逻辑的,但我无法调试it@shivgarg是的,如果我们谈论的是活动,当然你可以简单地写这个
。。ImageView im=新的ImageView(本);im.setImageResource(R.drawable.empty);gridView.setEmptyView(im);但并没有设置图像,整个屏幕变成蓝色,线性布局变得古怪。我知道这样问是不合逻辑的,但我无法调试it@shivgarg是的,如果我们谈论的是活动,当然你可以简单地写这个
。。ImageView im=新的ImageView(本);im.setImageResource(R.drawable.empty);gridView.setEmptyView(im);但是没有设置图像,整个屏幕变成蓝色,线性布局变得古怪。我知道这样问是不合逻辑的,但我无法调试它gridView.setEmptyView(findViewById(R.id.empty_list_view))代码>->gridView.setEmptyView(tvNoDataFound)代码>gridView.setEmptyView(findViewById(R.id.empty_列表_视图))代码>->gridView.setEmptyView(tvNoDataFound)代码>gridView.setEmptyView(findViewById(R.id.empty_列表_视图))代码>->gridView.setEmptyView(tvNoDataFound)代码>gridView.setEmptyView(findViewById(R.id.empty_列表_视图))代码>->gridView.setEmptyView(tvNoDataFound)代码>
GridView gridView = (GridView) findViewById( R.id.listhabbit_ma );
TextView tvNoDataFound = (TextView) findViewById(R.id.empty_list_view);
tvNoDataFound.setVisibility(View.VISIBLE);
gridView.setEmptyView( findViewById( R.id.empty_list_view ) );