Android自定义listviewrelativelayout定位问题

发布于 2025-01-06 17:17:10 字数 4094 浏览 2 评论 0原文

我对 Android 编程和布局比较陌生,并且一直致力于创建带有文本和图标的自定义列表视图。我已经检查并尝试了几个示例,并尝试从中创建我自己的列表视图。我现在正处于这样的时刻,我不知道我应该做什么才能让它像我想要的那样工作。

这是我当前的结果(第一行),第二行是 - 我丑陋的 MsPaint 版本 - 我想要的结果。如果文本到达放大镜图标,则文本将被裁剪,其后还有其他 2 个图标。

Current and Wanted State

我尝试了很多不同的布局来完成此任务,我被告知并读到我应该使用相对布局,因为它们具有更好的性能和其他东西(我现在不记得了)。

[编辑]更新和总结问题:我一直很困惑,但我认为我真正的问题是 textview 事先不知道我会在右侧放置更多图标的放大镜。这意味着它不会为这些图标“保留”任何空间,而只是占用所有可用空间,只为放大图标留下一些空间。[/编辑]

我有 3 个 xml 文件,其中包含我的布局。这是我的第一个文件,它只是我启动屏幕的布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/LayoutWrap">

    <ListView
        android:id="@+id/my_listview"
        style="@style/List"/>
</LinearLayout>

“my_listview”的“xml 文件”(我知道这是一个愚蠢的名称:)):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="@style/myListItem"
 android:id="@+id/list">


    <TextView
    android:id="@+id/my_item_title"
    style="@style/myItemName"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/my_item_detail"
    android:text="Name" />



    <ImageView
        android:id="@id/my_item_detail"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_detail_unselected"
        android:contentDescription="Detail"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/my_item_delete"
    />


    <ImageView
        android:id="@id/my_item_delete"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_delete_unselected"
        android:text="Delete my"
        android:layout_alignParentTop="true"
    />

        <ImageView
        android:id="@+id/my_item_copy"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_update_unselected"
        android:text="Copy my"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/my_item_delete"
    />

</RelativeLayout>

在我的“styles.xml”中定义了样式,其中没有任何特殊属性。我认为唯一有趣的是,我对具有特定 textSize 的文本进行了样式设置(也许 ?android:LargeAppearance 左右更好),并且我将其强制放在一行上。

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="WrapBoth">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="WrapHeight">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

        <style name="LayoutWrap">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>   
        <item name="android:orientation">vertical</item>        
    </style>

            <style name="List" parent="style/WrapHeight"/>

    <style name="myListItem">
        <item name="android:padding">5dp</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">?android:attr/listPreferredItemHeight</item>
    </style>

    <style name="myItemName" parent="style/WrapBoth">
        <item name="android:textSize">11sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:ellipsize">end</item>
        <item name="android:lines">1</item>
        <item name="android:scrollHorizontally">true</item>
    </style>

    <style name="myImageItem" parent="style/WrapBoth">

    </style>
</resources>

我已经很感谢您提前提供的任何帮助!同时,我将处理我的“buttonListeners”,我不认为它们会带来太大麻烦:)

I'm relative new to android programming and layouts, and got stuck on creating a custom listview with text and icons. I have checked and tried out several examples, and tried to make my own list view out of those. I'm now at such a point that I don't know what I should do to get this to work like I want it to.

Here's my current result (1st row), and the 2nd row is - my ugly MsPaint edition - of how I'd like it to be. Text which is cropped if it reaches the magnify-glass icon, with the 2 other icons after it.

Current and wanted state

I tried quite a few different layouts to accomplish this, and I've been told and read that I should use a relative layout, as those have a better performance and other thingies (which I can't recall right now).

[Edit]Update and summarized issue: I have been puzzling a bit more, but I think that my real problem is the fact that the textview does not know in advance that I will put more icons on the right of the magnify glass. This means that it does not 'reserve' any space for those icons, and just takes all the available space, and only leaves some for the magnify icon.[/Edit]

I have 3 xml files which contain my layout. Here's my first file, which is simply the layout I start the screen with:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/LayoutWrap">

    <ListView
        android:id="@+id/my_listview"
        style="@style/List"/>
</LinearLayout>

The 'xml-file' of 'my_listview' (I know it's a silly name :)):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="@style/myListItem"
 android:id="@+id/list">


    <TextView
    android:id="@+id/my_item_title"
    style="@style/myItemName"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/my_item_detail"
    android:text="Name" />



    <ImageView
        android:id="@id/my_item_detail"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_detail_unselected"
        android:contentDescription="Detail"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/my_item_delete"
    />


    <ImageView
        android:id="@id/my_item_delete"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_delete_unselected"
        android:text="Delete my"
        android:layout_alignParentTop="true"
    />

        <ImageView
        android:id="@+id/my_item_copy"
        style="@style/myImageItem"

        android:src="@drawable/ic_my_update_unselected"
        android:text="Copy my"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/my_item_delete"
    />

</RelativeLayout>

In my 'styles.xml' are the styles defined, which don't have any special properties. I think the only interesting thing is that I styled the text having a specific textSize (perhaps an ?android:LargeAppearance or so is better), and that I forced it on one line.

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="WrapBoth">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="WrapHeight">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

        <style name="LayoutWrap">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>   
        <item name="android:orientation">vertical</item>        
    </style>

            <style name="List" parent="style/WrapHeight"/>

    <style name="myListItem">
        <item name="android:padding">5dp</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">?android:attr/listPreferredItemHeight</item>
    </style>

    <style name="myItemName" parent="style/WrapBoth">
        <item name="android:textSize">11sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:ellipsize">end</item>
        <item name="android:lines">1</item>
        <item name="android:scrollHorizontally">true</item>
    </style>

    <style name="myImageItem" parent="style/WrapBoth">

    </style>
</resources>

I'm already grateful for any help in advance! Meanwhile, I'll work on my 'buttonListeners', I don't expect those to be much of a trouble :)

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

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

发布评论

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

评论(1

上课铃就是安魂曲 2025-01-13 17:17:10

在我更加习惯 android 和布局的工作方式之后,我最终能够解决这个问题。也许其他人可能会偶然发现同样的问题,并可能从中获得一些有用的信息。

为了解决这个问题,我创建了一个相对布局,其中包含 3 个图像,并按照我想要的方式放置图像。我给了我的relativelayout以下属性:

<RelativeLayout
        android:id="@id/options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
         >

我想保留在左侧的textview,我给了它以下属性:

<TextView
    android:id="@+id/item_title"
    style="@style/ItemName"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="@string/dummy_title" 
    android:layout_toLeftOf="@+id/options"/>

我使用的样式,我保持相同。
使用这种方式,布局知道应该为 3 个图像保留哪些空间。

这有点像数学中,括号中的内容先行,而在 Android 中,布局中的内容先行:)。

I've been able to solve this problem eventually, after I got more used to android and how the layouts work. And perhaps someone else might stumble upon the same issue and might get some useful info out of this.

To solve this issue, I created a relativelayout which had the 3 images inside, and position the images as I'd like them to be. I gave my relativelayout the following properties:

<RelativeLayout
        android:id="@id/options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
         >

And the textview I wanted to stay on the left, I gave it the following properties:

<TextView
    android:id="@+id/item_title"
    style="@style/ItemName"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="@string/dummy_title" 
    android:layout_toLeftOf="@+id/options"/>

The styles I used, I kept those the same.
Using this way, the layout knows what space should be reserved for the 3 images.

It's all a bit like in maths, stuff in brackets go first, and in android, stuff within layouts go first :).

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