简体   繁体   English

Android中的Android定位

[英]Android positioning in Layout

I'm trying to build in my Layout this kind of line: 我正在尝试在我的布局中构建这种线:

"Total......................1000" “总...................... 1000”

I need to change "1000" depending on what I receive from network. 我需要根据我从网络收到的内容更改“1000”。 As a result I have to change width of "........." . 结果我必须改变“.........”的宽度。 Word "Total" is just a final TextView. 单词“Total”只是最终的TextView。 How to implement it in Layout? 如何在布局中实现它? When phone changes to landscape "....." (dots) have to be longer and I don`t know how to implement it. 当手机更改为横向“.....”(点)必须更长时间,我不知道如何实现它。

So I have 3 parts: TextView "Total", TextView "...." (dots), and TextView "1000" which can be any namber from 1 to infinity. 所以我有3个部分:TextView“Total”,TextView“......”(点)和TextView“1000”,可以是从1到无穷大的任何namber。 How to implement them in order to show the mandatory "Total" and "1000" in conjunction with "....." between them depending on size of screen? 如何实现它们以根据屏幕大小显示强制性“总计”和“1000”以及它们之间的“.....”?

The following xml doesn`t make it. 以下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>

You need to play around with weight attributes : Try below snippet ,make use of below code and design as per your requirement : 您需要使用权重属性:尝试以下代码段,根据您的要求使用以下代码和设计:

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

Try this,, 尝试这个,,

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

Two little problems in my code, 我的代码中有两个小问题,

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.

How about a horizontal LinearLayout with the objects of equal layout_weights? 具有相同layout_weights的对象的水平LinearLayout怎么样? "Total" can be wrap_content width, "......" can be match_parent , and "1000" can be wrap_content . “total”可以是wrap_content width,“......”可以是match_parent ,“1000”可以是wrap_content Align "Total" to the left, "1000" to the right, and "....." will fill the rest of the space between the other two. 将“Total”对齐到左边,“1000”对齐,“.....”将填充其他两个之间的空间。 However, this would only work if you do something somewhat ridiculous like make the "...." string 200 dots long. 但是,这只有在你做一些有点荒谬的事情时才会起作用,例如将“......”字符串设置为200点长。 In that way, some of the dots will not appear and will run off screen, but at least you'll see all the dots you need between "Total" and "1000". 这样,一些点将不会出现并将在屏幕外运行,但至少你会看到“Total”和“1000”之间所需的所有点。 What do you think? 你怎么看?

The only other "more complicated" way I could think of doing this is every time you switch orientation or increase/decrease the number, find the width of the screen in pixels, figure out the width of the word "Total" and the each digit of the number in pixels, subtract that from the width, and divide that by the number of pixels a dot takes up. 我能想到的唯一其他“更复杂”的方法是每次切换方向或增加/减少数字,找到屏幕宽度(以像素为单位),找出单词“Total”的宽度和每个数字以像素为单位的数字,从宽度中减去该数字,并将其除以点占据的像素数。 That number would be the amount of dots to put in the "...." string. 该数字将是放在“....”字符串中的点数。

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

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