Android自定义控件布局问题

Android自定义控件布局问题,android,xml,layout,custom-controls,Android,Xml,Layout,Custom Controls,我已经构建了一个自定义listView xml项;然而,我并没有得到我所期望的…我改变了背景颜色,以更详细地显示第二张图片上发生的事情 基本上,播放列表项目应该延伸到屏幕的右侧,彩色文本应该全部在屏幕的右侧(这就是设计师正在展示的内容)。现在,彩色文本在歌曲标题的右侧对齐 如何解决此问题? 预期结果 我得到了什么 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas

我已经构建了一个自定义listView 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="64dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgSongThumbnail"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_weight="0"
    android:contentDescription="@string/hello_world"
    android:src="@drawable/crayonpop_barbarbar" />

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="0" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtSongName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="middle"
            android:singleLine="true"
            android:text="Bar Bar Bar Bar Bar Bar Bar"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#DDD" />

        <TextView
            android:id="@+id/txtGroupName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/group"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#DDD" 
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:layout_margin="8dp"
        android:gravity="bottom"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtSongMetaInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_weight="0"
            android:text="@string/meta_data"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/holo_blue_bright"
            android:textSize="12sp" />

    </LinearLayout>
</FrameLayout>

</LinearLayout>



更新

在此基础上,我重新编写了代码,遵循混合
线性布局
x
RelativeLayout
方案,然后我注意到这里弹出了一堆答案@yahya和我的答案几乎一样,所以我接受了这个答案,因为这个答案确实考虑到了我在上面的照片中展示的精确布局,并注意减少了大量边距代码的使用

下面是我手机上的代码(与下面@yahya的答案非常相似)。去吧……看看这些歌,尽情享受;)


我建议您使用相对布局,而不是线性布局。使用RelativeLayout,可以基于其他组件对齐组件

在这种情况下,

id为txtSongName的主文本视图应采用宽度作为匹配父项

txtGroupName
应使用
layout\u botton=@id/txtSongName

txtSongMetaInfo
应使用
layout\u-torightof=@id/txtGroupName
android:layout\u-alignParentRight=“true”


这样做的主要优点是减少了布局数量,从而提高了性能

我建议您使用相对布局而不是线性布局。使用RelativeLayout,可以基于其他组件对齐组件

在这种情况下,

id为txtSongName的主文本视图应采用宽度作为匹配父项

txtGroupName
应使用
layout\u botton=@id/txtSongName

txtSongMetaInfo
应使用
layout\u-torightof=@id/txtGroupName
android:layout\u-alignParentRight=“true”


这样做的主要优点是减少了布局数量,从而提高了性能

移除ImageView上的权重,将FrameLayout的权重更改为1,将宽度更改为0dp,就可以了。但是,正如@prijupaul所建议的,使用RelativeLayout来减少视图的数量,特别是当它位于ListView中时。顺便说一句,我建议您看看这个工具:。

删除ImageView上的权重,将FrameLayout的权重更改为1,宽度更改为0dp,这样就可以了。但是,正如@prijupaul所建议的,使用RelativeLayout来减少视图的数量,特别是当它位于ListView中时。顺便说一句,我建议您看看这个工具:。

我修改了您的布局,并减少使用的嵌入式布局以改进您的视图层次结构

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgSongThumbnail"
    android:layout_width="64dp"
    android:layout_height="64dp" 
    android:contentDescription="@string/hello_world"
    android:src="@drawable/crayonpop_barbarbar" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_margin="8dp"
    android:layout_height="match_parent"  >

    <TextView
        android:id="@+id/txtSongName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Bar Bar Bar Bar Bar Bar Bar"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#DDD" />

        <LinearLayout  
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtSongName"
            android:orientation="horizontal" 
            android:gravity="right" >


            <TextView
                android:id="@+id/txtSongMetaInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_weight="0"
                android:text="@string/meta_data"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@android:color/holo_blue_bright"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/txtGroupName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/group" 
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#DDD" 
                android:singleLine="true" />  

        </LinearLayout>
</RelativeLayout>

</LinearLayout>

我修改了您的布局,并减少了使用的嵌入式布局以改进您的视图层次结构

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgSongThumbnail"
    android:layout_width="64dp"
    android:layout_height="64dp" 
    android:contentDescription="@string/hello_world"
    android:src="@drawable/crayonpop_barbarbar" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_margin="8dp"
    android:layout_height="match_parent"  >

    <TextView
        android:id="@+id/txtSongName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Bar Bar Bar Bar Bar Bar Bar"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#DDD" />

        <LinearLayout  
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtSongName"
            android:orientation="horizontal" 
            android:gravity="right" >


            <TextView
                android:id="@+id/txtSongMetaInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_weight="0"
                android:text="@string/meta_data"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@android:color/holo_blue_bright"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/txtGroupName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/group" 
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#DDD" 
                android:singleLine="true" />  

        </LinearLayout>
</RelativeLayout>

</LinearLayout>

您的解决方案来了,只需复制以下代码即可满足您的要求

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/vw_grey_btn" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="false"
    android:layout_centerVertical="true"
    android:src="@drawable/logo" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="false"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/imageView1"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</LinearLayout>

</RelativeLayout>


由于我的声誉很低,我无法附上我的屏幕截图:

您的解决方案来了,只需复制以下代码即可满足您的要求

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/vw_grey_btn" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="false"
    android:layout_centerVertical="true"
    android:src="@drawable/logo" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="false"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/imageView1"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</LinearLayout>

</RelativeLayout>


由于我的声誉很低,我无法附上我的屏幕截图:

你的放大版面的代码是什么

您应该使用类似的方法进行充气(假设您使用的是BaseAdapter)

如果在膨胀时将null作为视图组根传递,则它将忽略根布局的fill\u parent/match\u parent参数,并将其设置为wrap\u content,从而给出所获得的结果


即使看起来你已经得到了你想要的布局,你也应该确保你仍然正确地膨胀它。

你膨胀布局的代码是什么

您应该使用类似的方法进行充气(假设您使用的是BaseAdapter)

如果在膨胀时将null作为视图组根传递,则它将忽略根布局的fill\u parent/match\u parent参数,并将其设置为wrap\u content,从而给出所获得的结果


即使看起来你已经得到了你想要的布局,你也应该确保你仍然正确地膨胀它。

事实上,通过将所有的子元素放在一个RelativeLayout中,可以用更少的视图完成整个布局:

<?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="64dp"
    android:background="#333" >

    <ImageView
        android:id="@+id/imgSongThumbnail"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/crayonpop_barbarbar" />

    <TextView
        android:id="@+id/txtSongName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@id/imgSongThumbnail"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Bar Bar Bar Bar Bar Bar Bar"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#DDD" />

    <TextView
        android:id="@+id/txtGroupName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/txtSongName"
        android:layout_below="@id/txtSongName"
        android:layout_marginBottom="8dp"
        android:singleLine="true"
        android:text="Sistar"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#DDD" />

    <TextView
        android:id="@+id/txtSongMetaInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/txtGroupName"
        android:layout_alignRight="@id/txtSongName"
        android:layout_gravity="end"
        android:layout_weight="0"
        android:text="Remix #1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="12sp" />

</RelativeLayout>

实际上,通过将所有子项放在一个RelativeLayout中,可以用更少的视图完成整个布局:

<?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="64dp"
    android:background="#333" >

    <ImageView
        android:id="@+id/imgSongThumbnail"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/crayonpop_barbarbar" />

    <TextView
        android:id="@+id/txtSongName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@id/imgSongThumbnail"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Bar Bar Bar Bar Bar Bar Bar"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#DDD" />

    <TextView
        android:id="@+id/txtGroupName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/txtSongName"
        android:layout_below="@id/txtSongName"
        android:layout_marginBottom="8dp"
        android:singleLine="true"
        android:text="Sistar"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#DDD" />

    <TextView
        android:id="@+id/txtSongMetaInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/txtGroupName"
        android:layout_alignRight="@id/txtSongName"
        android:layout_gravity="end"
        android:layout_weight="0"
        android:text="Remix #1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="12sp" />

</RelativeLayout>


注意:底部的XML与预期的结果图像相关。我希望这个XML布局基本上保持现在的布局……但如果这就是问题发生的原因,然后我可以处理一个不同的布局注意:底部的XML与预期的结果图像相关我希望这个XML布局基本上保持现在的布局……但是如果这就是为什么会出现这个问题,然后我可以处理不同的布局是的…我在考虑使用相对布局…但不确定如何使用
txtSongMetaInfo
。但我面临的更大问题是,我当前的代码似乎应该可以工作!我可能会做相对布局,但想知道为什么我的布局现在不起作用。我确实使用了
RelativeLayout
;然而,这是一个障碍!我仍然使用代码