android布局从屏幕推送视图

时间:2013-01-26 05:49:53

标签: android xml layout view

为了构建应用程序,我们有几个列表。 列表项存在问题,这是自定义的,但非常简单。 格式为:

enter image description here

这表示一个包含2个textviews和一个图像视图的列表项

请注意,标题和日期实际上位于彼此之下,图像位于右侧,中心垂直为属性。图像不应位于两个文本视图之间

我将首先提供XML,然后解释确切的问题。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

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

        <TextView 
            android:textSize="16dip" 
            android:id="@+id/title"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>

        <TextView 
            android:textSize="12dip" 
            android:id="@+id/date"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip" >

        <ImageView
            android:id="@+id/validationStateImg"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
</LinearLayout>

问题: 在某种意义上,此布局显示的内容与ascii表示完全相同。 什么不能正常工作是文本变长。如果文本很长,但不足以占用2行,则只会使图像视图变小。

在其他情况下,它只是将imageview完全推离屏幕..

我需要的是,当日期或其他文本视图的长度太长时,要打破一个新行。当然,它需要是一种可以移植到各种屏幕尺寸的解决方案。

我不是UI艺术家,所以,如果我滥用布局,不应该使用它们,请道歉。

除了帮助,我们也欢迎提示和提示!

4 个答案:

答案 0 :(得分:6)

我认为你最好的选择是一个简单的RelativeLayout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    >
    <ImageView
        android:id="@+id/validationStateImg"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        />
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/validationStateImg"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="16dp"
        />
    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/validationStateImg"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/title"
        />
</RelativeLayout>

将ImageView捕捉到父级的右侧,让TextViews占据宽度的其余部分,但是与ImageView的左侧对齐。

答案 1 :(得分:2)

希望这会有所帮助:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:layout_weight = "1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dasdfasdfasdfasdfsadfsdf dsfasdfasd asdfadsf dfasdfads asdfasdf"
        android:textSize="16dip" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dasdfasdfasdfasdfsadfsdf"
        android:textSize="12dip" />
</LinearLayout>

<RelativeLayout
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_marginRight="5dip" >

    <ImageView
        android:id="@+id/validationStateImg"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

答案 2 :(得分:1)

我认为在这种情况下你需要一个不同的布局,请检查一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView 
           android:textSize="16dip" 
           android:id="@+id/title"
           android:layout_width="match_parent" 
           android:layout_height="wrap_content"/>
        <TextView 
           android:textSize="12dip" 
           android:id="@+id/date"
           android:layout_width="match_parent" 
           android:layout_height="wrap_content"/>
    </LinearLayout>
    <ImageView
       android:id="@+id/validationStateImg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical"/>

</LinearLayout>

答案 3 :(得分:0)

如果您不希望图像缩小,那么您需要为其父母提供的房地产提供特定比率。

具体而言,作为其父级的LinearLayout需要具有weight_sum,然后文本和图像都需要具有layout_weight。

重量应加起来。权重不一定是一个整数,并且非常重要(并且非常反直觉)layout_width需要为0(假设它们中的两个并排坐着)......所以......

我正在删除下面你需要添加的所有额外的东西,只是留下重要的部分....

<LinearLayout weight_sum=2 layout_height=fill_parent>
   <TextView layout_weight=2 layout_height=0dp/>
   <ImageView layout_weight=2 layout_height=0dp />
   <TextView layout_weight=2 layout_height=0dp/>
</LinearLayout>

这会将LinearLayout分成两半,左侧将是文本(如果可以,将会换行),右侧将是图像。您可以通过将总和设置为3并将边分割为2和1来调整此值,在这种情况下,文本将是屏幕的2/3和图像的1/3。或者将总和保留为2,左边为1.3,右边为.7,效果相似。