Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带按钮的Android ListViewItem布局_Java_Android_Android Layout_Listview_Android Listview - Fatal编程技术网

Java 带按钮的Android ListViewItem布局

Java 带按钮的Android ListViewItem布局,java,android,android-layout,listview,android-listview,Java,Android,Android Layout,Listview,Android Listview,我对安卓系统还很陌生。我想在右下角的listview项目模板中添加两个按钮 现在我有这样的布局: 以及守则: 我想要实现的是在右下角添加两个按钮,如下所示: 我还尝试将RelativeLayout放入LinearLayout,然后再添加一个子LinearLayout,按钮将其水平对齐,但listview项目行根本不显示它们。您可以使用此代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi

我对安卓系统还很陌生。我想在右下角的listview项目模板中添加两个按钮

现在我有这样的布局:

以及守则:


我想要实现的是在右下角添加两个按钮,如下所示:


我还尝试将
RelativeLayout
放入
LinearLayout
,然后再添加一个子
LinearLayout
,按钮将其水平对齐,但listview项目行根本不显示它们。

您可以使用此代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Test title"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toRightOf="@id/icon"
        android:singleLine="true"
        android:text="Details"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:layout_marginLeft="2dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#000000"
       android:layout_marginLeft="2dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
</LinearLayout>


根据您的需要修改代码

如果我理解您的意思,您需要每个列表项右下角的按钮。如果是这样,以下内容将适用于您:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height= "fill_parent">
   <RelativeLayout
         android:id="@+id/FirstLayout"
         android:layout_width="fill_parent"
         android:layout_height= "fill_parent">
           <ImageView
               android:id="@+id/icon"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_alignParentBottom="true"
               android:layout_alignParentTop="true"
               android:layout_marginRight="6dip"
               android:src="@drawable/ic_launcher" />

           <TextView
               android:id="@+id/secondLine"
               android:layout_width="fill_parent"
               android:layout_height="26dip"
               android:layout_alignParentBottom="true"
               android:layout_alignParentRight="true"
              android:layout_toRightOf="@id/icon"
               android:ellipsize="marquee"
               android:singleLine="true"
               android:text="Details"
               android:textSize="12sp" />

           <TextView
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_above="@id/secondLine"
               android:layout_alignParentRight="true"
               android:layout_alignParentTop="true"
               android:layout_alignWithParentIfMissing="true"
               android:layout_toRightOf="@id/icon"
               android:gravity="center_vertical"
               android:text="Test title"
               android:textSize="16sp" />
     </RelativeLayout>
     <RelativeLayout
         android:id="@+id/SecondLayout"
         android:layout_below="@+id/FirstLayout"
         android:layout_width="fill_parent"
         android:layout_height= "fill_parent">
              <Button 
                  android:id="@+id/btnTwo"
                  android:text ="Button Two"
                  android:layout_alignParentRight="true"/>
              <Button 
                  android:id="@+id/btnOne"
                  android:text ="Button One"
                  android:layout_toLeftOf="@+id/btnTwo"/>
      </RelativeLayout>
</RelativeLayout>


祝你好运

将以下代码用于所需布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DADADA" >

    <RelativeLayout
        android:id="@+id/ItemMainContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/Icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/Icon"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test title" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Details" />
        </LinearLayout>
    </RelativeLayout>

    <View
        android:id="@+id/HorizontalSeparator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/ItemMainContainer"
        android:layout_margin="5dp"
        android:background="#CCCCCC" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/HorizontalSeparator"
        android:layout_margin="2dp" >

        <TextView
            android:id="@+id/Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="5dp"
            android:clickable="true"
            android:drawableLeft="@drawable/delete"
            android:gravity="center"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="Button2" />

        <View
            android:id="@+id/Separator"
            android:layout_width="1dp"
            android:layout_height="27dp"
            android:layout_toLeftOf="@id/Button2"
            android:background="#CCCCCC" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_toLeftOf="@id/Separator"
            android:clickable="true"
            android:drawableLeft="@drawable/delete"
            android:gravity="center"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="Button1" />
    </RelativeLayout>

</RelativeLayout>

我还附上了结果的截图


相应地更改资源。

如果我没有错,您正在寻找组件的对齐调整。您已经使用了android:layout\u alignParentXXXX和layout\u上/下属性。 请做一些改变,你会得到想要的

我已经检查了下面的代码,这可能会帮助你

<?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="match_parent" >

    <RelativeLayout
        android:id="@+id/rel2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/lin1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
// here place you existing code
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lin1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</RelativeLayout>

//在这里放置现有代码


将代码与父布局放在一起..请显示一些代码。我想问一下要在其中显示按钮的行布局代码。您是否使用自定义适配器?尝试将
图像视图
和另外两个
文本视图
封装为一个
线性布局
,将两个
按钮
封装为另一个
线性布局
。将一个
RelativeLayout
作为两者的父级,并使用
layout_将它们放置在
属性下方。
<?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="match_parent" >

    <RelativeLayout
        android:id="@+id/rel2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/lin1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
// here place you existing code
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lin1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</RelativeLayout>
<?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="wrap_content"
android:orientation="vertical" >

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <ImageView 
        android:layout_width="50dp"
        android:layout_height="50dp" 
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"/>

        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Description"/>
        </LinearLayout>

</LinearLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right|center_vertical">

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Button2"/>

     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:padding="5dp"
        android:text="Button1"/>
</LinearLayout>
</LinearLayout>