无法为 ListView 设置背景

发布于 2024-12-05 22:54:48 字数 1757 浏览 0 评论 0原文

我正在尝试为我的列表视图设置背景。我从可绘制对象中获取了一张图像并将其设置为布局文件的背景,然后它给出的结果如下屏幕截图

,当我删除背景时,我得到了我需要的东西,但没有背景。 屏幕截图是 没有背景的屏幕 是否有任何其他语法来设置 ListView 的背景。

我的布局代码是

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"

      >


    <TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/label"
            android:textColor="#FF0000"
            android:textSize="30px"></TextView>
      </LinearLayout>




And my java code and that is 

String[] names = new String[] { "India", "Malaysia" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen3,
                R.id.label, names));
    }

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        if(keyword.equals("India"))
        {

            Intent ima1=new Intent(screen3.this,new21.class);

            startActivity(ima1);

        }
        else
        {
            Intent ima2=new Intent(screen3.this,new22.class);
            startActivity(ima2);
        }

        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();

    }

最后我需要的是我想要带有正确列表视图的背景图像,显示一个又一个列表项。提前致谢 }

I am trying to set background for my List view . I have taken a image from the drawable and set it as background to the layout file then it is giving the result as following screen shotscreen with background

and when i just remove the background am getting the thing which i required but no background.
and screen shot is screen without background
Is there is any other Syntax to set background for ListView.

And my Code for layout is

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"

      >


    <TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/label"
            android:textColor="#FF0000"
            android:textSize="30px"></TextView>
      </LinearLayout>




And my java code and that is 

String[] names = new String[] { "India", "Malaysia" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen3,
                R.id.label, names));
    }

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        if(keyword.equals("India"))
        {

            Intent ima1=new Intent(screen3.this,new21.class);

            startActivity(ima1);

        }
        else
        {
            Intent ima2=new Intent(screen3.this,new22.class);
            startActivity(ima2);
        }

        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();

    }

And lastly i need is I want the background image with proper listview showing one listitem after another. Thanks in advance
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-12-12 22:54:48

尝试此代码,您可以设置包含列表视图的布局和列表视图内容的单独布局,而不是扩展 ListActivity。

Java 代码:

public class Table_Layout extends Activity {

  ListView list_view;
  @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.table_layout);
      list_view=(ListView)findViewById(R.id.list_view);
      String[] names = new String[] { "India", "Malaysia" };
      list_view.setAdapter(new ArrayAdapter<String>(this, R.layout.screen3,R.id.label, names));
    }
}

包含列表视图的主 XML:table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" android:background="@drawable/uploading">
    
    <ListView   android:layout_width="fill_parent" android:layout_height="fill_parent" 
                android:id="@+id/list_view"  
    />

然后列表视图的内容:screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
<TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/label"
        android:textColor="#FF0000"
        android:textSize="30px"></TextView>
  </LinearLayout>

设置 table_layout.xml 的背景,以便它将按您的预期应用于整个列表视图。

Try this code, Instead of extending ListActivity you can set layout that contains List View and separate layout for contents of List View.

Java Code :

public class Table_Layout extends Activity {

  ListView list_view;
  @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.table_layout);
      list_view=(ListView)findViewById(R.id.list_view);
      String[] names = new String[] { "India", "Malaysia" };
      list_view.setAdapter(new ArrayAdapter<String>(this, R.layout.screen3,R.id.label, names));
    }
}

Main XML that contains List View :table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" android:background="@drawable/uploading">
    
    <ListView   android:layout_width="fill_parent" android:layout_height="fill_parent" 
                android:id="@+id/list_view"  
    />

Then Contents of List View :screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
<TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/label"
        android:textColor="#FF0000"
        android:textSize="30px"></TextView>
  </LinearLayout>

Set you Background for the table_layout.xml so that it will applied for entire List View as you expected.

冰之心 2024-12-12 22:54:48

您的 xml 文件看起来一切都很好。尝试实现下面提到的: -

制作一个您想要列表视图每行大小的图像。并将其设置为父LinearLayout的背景。设置:- android:layout_width="fill_parent" android:layout_height="wrap_content"。

希望这有帮助! :)

All looks well with your xml file. Try implementing the below mentioned:-

Make an image of the size you want your listview's each row of. And set it as the parent LinearLayout's background. Set:- android:layout_width="fill_parent" android:layout_height="wrap_content".

Hope this helps !! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文