Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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
Android 将版面设置为其内容的大小_Android_Android Layout_Android Linearlayout_Android Xml - Fatal编程技术网

Android 将版面设置为其内容的大小

Android 将版面设置为其内容的大小,android,android-layout,android-linearlayout,android-xml,Android,Android Layout,Android Linearlayout,Android Xml,我有一个简单的XML,我正试图膨胀,并努力与布局。我想要达到的目标很简单(或者我是这么想的),所以我肯定我做得不对。基本上我有三个元素,两个文本视图和一个图像视图。我想把两个文本框放在另一个上面,图像在它们的右边,对齐到右边,它的视图适合图像的大小。像这样: Text 1 |==| Text 2 would be below text 1 |==| 以下是我的XML: <?xml version="1.0" encoding="utf

我有一个简单的XML,我正试图膨胀,并努力与布局。我想要达到的目标很简单(或者我是这么想的),所以我肯定我做得不对。基本上我有三个元素,两个文本视图和一个图像视图。我想把两个文本框放在另一个上面,图像在它们的右边,对齐到右边,它的视图适合图像的大小。像这样:

    Text 1                     |==| 
Text 2 would be below text 1   |==|
以下是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout

        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

问题是,如果不对包含两个文本框的LinearLayout的大小进行硬编码,就无法显示图像

感谢您的帮助

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
         android:layout_width="0dp"                  //changes here
         android:layout_weight="0.6"                 //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"              //changes here
        android:layout_weight="0.4"                //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
         android:layout_width="0dp"                  //changes here
         android:layout_weight="0.6"                 //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"              //changes here
        android:layout_weight="0.4"                //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
         android:layout_width="0dp"                  //changes here
         android:layout_weight="0.6"                 //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"              //changes here
        android:layout_weight="0.4"                //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
         android:layout_width="0dp"                  //changes here
         android:layout_weight="0.6"                 //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"              //changes here
        android:layout_weight="0.4"                //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

//试试这个
//试试这个
//试试这个
//试试这个

始终使用相对布局,与其他布局相比,它需要更少的代码

试试下面,我想这就是你想要的,根据你的要求设置填充/边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <TextView
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="@dimen/tab_host_default_height"
        android:text="@string/test_string"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="@string/test_string" />

    <ImageButton
        android:id="@+id/entry3"
        style="@style/ImageButtonBDTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/click_for_repair_report"
        android:paddingRight="5dp"
        android:src="@drawable/entry_link" />

</RelativeLayout>

始终使用相对布局,与其他布局相比,它需要更少的代码

试试下面,我想这就是你想要的,根据你的要求设置填充/边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <TextView
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="@dimen/tab_host_default_height"
        android:text="@string/test_string"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="@string/test_string" />

    <ImageButton
        android:id="@+id/entry3"
        style="@style/ImageButtonBDTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/click_for_repair_report"
        android:paddingRight="5dp"
        android:src="@drawable/entry_link" />

</RelativeLayout>

始终使用相对布局,与其他布局相比,它需要更少的代码

试试下面,我想这就是你想要的,根据你的要求设置填充/边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <TextView
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="@dimen/tab_host_default_height"
        android:text="@string/test_string"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="@string/test_string" />

    <ImageButton
        android:id="@+id/entry3"
        style="@style/ImageButtonBDTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/click_for_repair_report"
        android:paddingRight="5dp"
        android:src="@drawable/entry_link" />

</RelativeLayout>

始终使用相对布局,与其他布局相比,它需要更少的代码

试试下面,我想这就是你想要的,根据你的要求设置填充/边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <TextView
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="@dimen/tab_host_default_height"
        android:text="@string/test_string"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="@string/test_string" />

    <ImageButton
        android:id="@+id/entry3"
        style="@style/ImageButtonBDTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/click_for_repair_report"
        android:paddingRight="5dp"
        android:src="@drawable/entry_link" />

</RelativeLayout>



谢谢,但是设置权重不适合图像的大小。我试图使包含视图的布局与图像+填充的精确大小相同,而包含文本的线性布局使用rest@xceph然后,将图像布局宽度指定为换行内容,将文本布局权重指定为1个字符,但设置权重并不适合图像的大小。我试图使包含视图的布局与图像+填充的精确大小相同,而包含文本的线性布局使用rest@xceph然后,将图像布局宽度指定为换行内容,将文本布局权重指定为1个字符,但设置权重并不适合图像的大小。我试图使包含视图的布局与图像+填充的精确大小相同,而包含文本的线性布局使用rest@xceph然后,将图像布局宽度指定为换行内容,将文本布局权重指定为1个字符,但设置权重并不适合图像的大小。我试图使包含视图的布局与图像+填充的精确大小相同,而包含文本的线性布局使用rest@xceph然后将图像布局宽度指定为包裹内容,将文本布局权重指定为1!谢谢你的快速回复:)你能解释一下这是怎么回事吗?我本以为在其中一个中设置权重,但在另一个中设置权重只会显示该元素。显示imagebutton所需的大小是多少,其余大小由textview布局使用..明白了!谢谢你的快速回复:)你能解释一下这是怎么回事吗?我本以为在其中一个中设置权重,但在另一个中设置权重只会显示该元素。显示imagebutton所需的大小是多少,其余大小由textview布局使用..明白了!谢谢你的快速回复:)你能解释一下这是怎么回事吗?我本以为在其中一个中设置权重,但在另一个中设置权重只会显示该元素。显示imagebutton所需的大小是多少,其余大小由textview布局使用..明白了!谢谢你的快速回复:)你能解释一下这是怎么回事吗?我本以为在其中一个中设置权重,但另一个不会只显示该元素。显示imagebutton所需的大小是多少,其余的大小由textview布局使用。谢谢,这也有效,而且确实小得多。谢谢,这也有效,而且确实小得多。谢谢,这也有效,而且确实小得多。谢谢,这也有效,而且确实小得多。