将布局设置为其内容的大小

时间:2013-10-09 12:21:55

标签: android android-layout android-linearlayout android-xml

我有一个简单的XML,我试图膨胀,并努力与布局。我想要实现的是非常简单(或者我认为),所以我确信我会错误地解决它。基本上我有三个元素,2个TextView和一个Imageview。我希望将两个文本框放在一起,将图像放在右侧,与右侧对齐,并使其视图与图像的大小相符。像这样:

    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的大小进行硬编码,我就无法显示图像。

感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

试试这个..

<?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>

答案 1 :(得分:1)

// try this
<?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="match_parent"
              android:layout_height="wrap_content"
              android:padding="5dp"
              android:minHeight="?android:attr/listPreferredItemHeight"
              android:paddingRight="?android:attr/scrollbarSize"
              android:gravity="center">

    <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            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: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:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical">

        <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>

答案 2 :(得分:1)

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

尝试以下,我想这就是你想要的,根据你的要求设置填充/保证金。

<?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>