2012-07-03 29 views
0

我想在我的Layout中构建这种线:Android在布局中的定位

“Total ...................... 1000 “

我需要根据我从网络接收的内容更改”1000“。因此我必须改变“.........”的宽度。 Word“Total”只是一个最终的TextView。如何在布局中实现它?当手机改变为风景“.....”(点)必须更长,我不知道如何实现它。因此,我有3个部分:TextView“Total”,TextView“....”(点)和TextView“1000”,它们可以是从1到无穷大的任何namber。如何实现它们,以便根据屏幕的大小显示强制的“Total”和“1000”以及它们之间的“.....”?

下面的xml没有做到这一点。

<RelativeLayout 
       android:id="@+id/statistic_layout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/statistic_separator" 
       android:orientation="horizontal" 
       android:padding="15dp"> 


       <TextView android:id="@+id/published_recepies" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textStyle="bold" 
         android:textSize="18dp" 
         android:layout_alignParentLeft="true" 
         android:textColor="@color/dark_orange" 
         android:text="Total"/> 

       <TextView android:id="@+id/dots" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textSize="18dp" 
         android:layout_toRightOf="@+id/published_recepies" 
         android:textColor="@color/dark_orange" 
         android:text="@string/dots"/> 

       <TextView android:id="@+id/published_recepies_data" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="18dp" 
         android:layout_alignParentRight="true" 
         android:textColor="@color/dark_orange" 
         android:text="323"/>   

      </RelativeLayout> 

回答

1

试试这个,,

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:background="@android:color/white" 
     android:singleLine="true" 
     android:text="......................................................................................................................................................................" 
     android:textColor="#000000" 
     android:textSize="18dip" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:background="@android:color/white" 
     android:text="Total" 
     android:textColor="#000000" 
     android:textSize="18dip" /> 

    <TextView 
     android:id="@+id/text3" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="right" 
     android:background="@android:color/white" 
     android:text="1000" 
     android:textColor="#000000" 
     android:textSize="18dip" /> 

</FrameLayout> 

两个小问题在我的代码中,

1. You need to write so many dots hardcoded in layout xml file. 
2. As sometimes there is a some little portion of dots cuts on both side. 
+0

我尝试过 - 这有效。非常感谢你 – Stas

+0

欢迎好友..!快乐编码..!如果我为第二个问题找到任何解决方案,我肯定会更新我的答案。 – user370305

+0

谢谢。我感谢你的帮助。 – Stas

0

水平方向LinearLayout与layout_weights相等的对象怎么样? “Total”可以是wrap_content宽度,“......”可以是match_parent,“1000”可以是wrap_content。将“总数”对齐到左边,将“1000”对齐到右边,并且“.....”将填充另外两个之间的剩余空间。然而,这只有在你做一些有点荒谬的事情时才会起作用,比如使“......”字符串长200个点。这样一来,一些点就不会出现在屏幕上,但至少你会在“Total”和“1000”之间看到你需要的所有点。你怎么看?

我可以想到的唯一的其他“更复杂”的方式是每次切换方向或增加/减少数字,以像素为单位查找屏幕宽度,找出单词“Total”的宽度,以像素为单位的数字的每个数字,从宽度中减去该数字,并将其除以点所占的像素数量。这个数字应该是放在“....”字符串中的点的数量。

+0

LinearLayout中的android:layout_alignParentRight =“true”是abcent ...引力=“right”呢?可以吗?你说什么听起来都很好,但我必须尝试。 – Stas

+0

啊,是的,我的道歉;尝试一个水平的RelativeLayout。如果这仍然失败,那么在一个封装RelativeLayout中的3个TextView每个都在它们自己的LinearLayout中应该可以工作。 – Mxyk

+0

我想我已经完成了你所说的,不幸的是我没有工作,因为可以看到所有的点(他们的长线)。我编辑了答案,以便更具体地说话。 – Stas

1

你需要玩弄权重属性:尝试以下段时,下面的代码和设计使用的按照您的要求:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="#ffffff" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="#ffffeabc" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:background="#ffffedef" > 
</LinearLayout> 

</LinearLayout>