【问题标题】:Android: Justify text to the right in nested Linear LayoutAndroid:在嵌套线性布局中向右对齐文本
【发布时间】:2014-06-03 15:37:26
【问题描述】:

我很难在 Android 中将 TextViews 放在左侧。我看了here,但没有成功。我要证明的 TextView 位于嵌套在父线性布局中的线性布局中:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <Button
            android:text="New Button"
            android:id="@+id/button1"
            style="@style/calibrate_button" />

        <TextView
            android:text="@string/calibrate"
            android:id="@+id/textView3"
            style="@style/calibrate_text" />
    </LinearLayout>

风格: @白颜色 20sp 包装内容 包装内容 ?android:attr/textAppearanceLarge 正确的

<style name="calibrate_button">
    <item name="android:background">#fcfcfc</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

【问题讨论】:

  • 为什么不使用RelativeLayout?
  • 我认为在这种情况下水平 LinearLayout 将是可行的方法。我有一个按钮和文本视图,我想水平排列。不过我会研究一下RelativeLayout。
  • 你想做什么?该 xml 将在左侧创建一个按钮,在其右侧创建一个 TextView
  • 我希望按钮位于屏幕左侧,文本视图位于右侧。这会在屏幕左侧创建一个按钮和文本视图
  • 那么你应该使用RelativeLayout,这就是它的用途:它具有将事物相对于其父对象或其他事物对齐的属性。无论如何为什么 layout_gravity 属性?也许你的意思是重力?

标签: android xml android-layout


【解决方案1】:

Lik Gil 说,你可以这样用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <Button
        android:id="@+id/button1"
        style="@style/calibrate_button"
        android:layout_alignParentLeft="true"
        android:text="New Button" />

    <TextView
        android:id="@+id/textView3"
        style="@style/calibrate_text"
        android:text="@string/calibrate"
        android:layout_alignParentRight="true" />

</RelativeLayout>

【讨论】:

  • 正如我上面评论的那样,我不知道 OP 是如何使用这种布局的,但是这里不需要 layout_gravity="center_horizontal"。这将使布局在父级中居中(在此示例中没有父级)。要将按钮布局中居中,请使用gravity="center_horizontal"
  • 他说他想证明一个 TextView 在线性布局中嵌套在父线性布局中。
猜你喜欢
  • 2014-10-24
  • 2011-12-02
  • 2014-01-14
  • 1970-01-01
  • 2011-05-17
  • 2018-03-27
  • 1970-01-01
  • 2022-01-19
  • 2011-10-22
相关资源
最近更新 更多