简体   繁体   English

如何让TextView不将ImageView推离屏幕

[英]How to get a TextView to not push an ImageView off the screen

I'm dynamically generating a layout, and to do this I have a menu_row_element.xml file which contains the repeating row structure. 我正在动态生成布局,为此,我有一个menu_row_element.xml文件,其中包含重复的行结构。

It consists of a LinearLayout (horizontal) which contains an image, a larger text above a smaller one, and an ImageView with a right arrow (inside another LinearLayout to keep it right aligned). 它由一个包含图像的LinearLayout(水平),一个较小的文本上方的较大文本和一个右箭头的ImageView(另一个LinearLayout内部保持右对齐)组成。

When the text part is small, everything looks fine. 当文本部分很小时,一切看起来都很好。 However if the text part is long, the right arrow is pushed off the screen. 但是,如果文本部分很长,则右箭头会从屏幕上移开。

How can I keep the right arrow always right aligned on the screen and have the textviews wrap the text appropriately? 如何让右箭头始终在屏幕上对齐,并使文本视图适当地包装文本?

In this image, the last row is OK since the text is small, but rows 1 and 2 push the right arrow off the screen. 在此图像中,最后一行是正常的,因为文本很小,但第1行和第2行将右箭头推离屏幕。

a busy cat http://img706.imageshack.us/img706/2927/device20111221203115.jpg 一只忙碌的猫http://img706.imageshack.us/img706/2927/device20111221203115.jpg

The xml file is: xml文件是:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:clickable="true"
        android:background="@drawable/main_menu_button"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:orientation="horizontal"
        android:gravity="left|center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!-- Icon of each section -->
        <ImageView
            android:id="@+id/menu_icon"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:padding="10px" />

        <!-- Text, in two parts -->
        <LinearLayout
            android:orientation="vertical"
            android:paddingLeft="10px"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent">

            <!-- Big font text -->
            <TextView
                android:id="@+id/menu_element_big_text"
                android:textSize="20dp"
                android:textColor="@color/black"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <!-- Small font text -->
            <TextView
                android:id="@+id/menu_element_small_text"
                android:scrollHorizontally="false"
                android:textSize="15dp"
                android:textColor="@color/black"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <!-- Layout just to be able to right align, sheesh! -->
        <LinearLayout
            android:gravity="right|center_vertical"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:padding="0dp"
                android:src="@drawable/right" />
        </LinearLayout>

    </LinearLayout>

Thanks! 谢谢!

Use a RelativeLayout . 使用RelativeLayout

The structure should be then: 结构应该是:

<LinearLayout> <!--This is the outer one and will contain everything else-->
       <RelativeLayout> <!-- width: fill, height: wrap, one of these for each row-->
           <ImageView 
              android:id="@+id/rightArrow"
              android:layout_alignParentRight="true"/> <!-- This is the arrow that points to the right wrap width and height-->
           <ImageView
              android:id="@+id/icon"
              android:layout_alignParentLeft="true"/><!--This is the icon on the left wrap width and height-->
           <TextView
              android:id="@+id/largeText"
              android:text="Large Text"
              android:layout_alignParentBottom="true"
              android:layout_toLeftOf="@+id/rightArrow"
              android:layout_toRightOf="@+id/icon"/> <!-- fill width and wrap height-->
           <TextView
              android:id="@+id/smallText"
              android:text="Small Text"
              android:layout_below="@+id/largeText"
              android:layout_toLeftOf="@+id/rightArrow"
              android:layout_toRightOf="@+id/icon"/> <!-- fill width and wrap height-->
        </RelativeLayout>
    </LinearLayout>

Or if you prefer (and this is actually probably better), do this: 或者如果您愿意(这实际上可能更好),请执行以下操作:

<LinearLayout> <!--This is the outer one and will contain everything else-->
   <RelativeLayout> <!-- width: fill, height: wrap, one of these for each row-->
       <ImageView 
          android:id="@+id/rightArrow"
          android:layout_alignParentRight="true"/> <!-- This is the arrow that points to the right wrap width and height-->
     <LinearLayout android android:layout_toLeftOf="@+id/rightArrow"
          ><!--orientation:horizontal, fill width, wrap height-->
       <ImageView
          android:id="@+id/icon"/><!--This is the icon on the left wrap width and height-->
       <TextView
          android:id="@+id/largeText"
          android:text="Large/> <!-- fill width and wrap height-->
       <TextView
          android:id="@+id/smallText"
          android:text="Small Text"/> <!-- fill width and wrap height-->
     </LinearLayout>
    </RelativeLayout>
</LinearLayout>

You can keep your linear layout. 您可以保持线性布局。 Just give a layout weight value of 1 to the text block. 只需将布局权重值1赋予文本块即可。 You don't need to put your imageview inside another layout this way. 您不需要以这种方式将imageview放在另一个布局中。

You'll want to limit your textviews to a single line as well. 您还希望将文本视图限制为单行。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
android:background="@drawable/main_menu_button"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="horizontal"
android:gravity="left|center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<!-- Icon of each section -->
<ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
    android:padding="10px" />

<!-- Text, in two parts -->
<LinearLayout
    android:orientation="vertical"
    android:paddingLeft="10px"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1">

            <!-- Big font text -->
    <TextView
        android:id="@+id/menu_element_big_text"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:textSize="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <!-- Small font text -->
    <TextView
        android:id="@+id/menu_element_small_text"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:textSize="15dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:padding="0dp"
    android:src="@drawable/right" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM