在机器人布局左和右边缘对齐textviews [英] Aligning textviews on the left and right edges in Android layout

查看:127
本文介绍了在机器人布局左和右边缘对齐textviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

入门Android和无法得到一个简单的布局去。

我想用的LinearLayout在单行位二TextViews。一个TextView的左手侧,另一个在右边(类似于浮动:左,浮动:权利CSS)

这是可能的或者我需要使用不同的ViewGroup或进一步布局嵌套来完成它?

这是我到目前为止有:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:方向=横向机器人:填充=10SP>
< TextView的机器人:ID =@ + ID / mytextview1机器人:layout_height =WRAP_CONTENT机器人:文本=somestringontheleftSomestring机器人:layout_width =WRAP_CONTENT/>
< TextView的机器人:ID =@ + ID / mytextview2机器人:layout_height =WRAP_CONTENT机器人:ellipsize =终结
    机器人:文本=somestringontheright机器人:layout_width =WRAP_CONTENT/>
< / LinearLayout中>
 

解决方案

使用一个<一个href="http://developer.android.com/intl/fr/reference/android/widget/RelativeLayout.html"><$c$c>RelativeLayout与<一个href="http://developer.android.com/intl/fr/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android%3Alayout_alignParentLeft"><$c$c>layout_alignParentLeft和<一href="http://developer.android.com/intl/fr/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android%3Alayout_alignParentRight"><$c$c>layout_alignParentRight:

 &LT; RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / RelativeLayout01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=10dp&GT;

    &LT;的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:ID =@ + ID / mytextview1/&GT;

    &LT;的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:ID =@ + ID / mytextview2/&GT;

&LT; / RelativeLayout的&GT;
 

此外,<一个href="http://developer.android.com/intl/fr/guide/practices/screens_support.html#density-independence">you或许应该使用(或 DP )而不是 SP 在布局。 SP 反映文本设置以及屏幕像素密度所以他们通常只对文字大小的物品。

Getting started with Android and having trouble getting a simple layout going.

I would like to use a LinearLayout to position two TextViews in a single row. One TextView on the left hand side, the other on the right hand side (analogous to float:left, float:right in CSS).

Is that possible or do I need to use a different ViewGroup or further layout nesting to accomplish it?

Here's what I have so far:

<?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" android:padding="10sp">
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/>
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end"
    android:text="somestringontheright" android:layout_width="wrap_content"/>
</LinearLayout>

解决方案

Use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="10dp">

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:id="@+id/mytextview1"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:id="@+id/mytextview2"/>

</RelativeLayout>

Also, you should probably be using dip (or dp) rather than sp in your layout. sp reflect text settings as well as screen density so they're usually only for sizing text items.

这篇关于在机器人布局左和右边缘对齐textviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆