【问题标题】:align buttons with texts to the left side xml android将带有文本的按钮对齐到左侧xml android
【发布时间】:2015-07-17 17:11:50
【问题描述】:

在这段代码中,我需要将 Textview 的按钮向左对齐,我尝试使用everythnin ,使用相对布局、重力等......

我现在不知道为什么这仍然对齐到水平线的中心,我需要在左侧!

<?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:background="@color/dark_background"
android:orientation="vertical"
 android:gravity="left" >

         <ScrollView android:id="@+id/ScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout    
   android:gravity="left"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
 android:padding="6dip"  
 >
 <TextView  
    android:id="@+id/help_page_intro"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/help_page_intro"
    android:padding="2dip"
    android:layout_weight="1"
       />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="4dip"
    android:layout_gravity="left" 
    >
    <Button android:id="@+id/help_button1"
    android:layout_weight="1"
    android:layout_width="180dip"
    android:layout_height="wrap_content"
        style="@style/HelpButton.Dark"
        android:onClick="onClickHelp"
        android:text="@string/help_title_section1"
        android:drawableTop="@drawable/help_image1"/>
    <TextView  
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/help_text_section1"
    android:padding="8dip"
    android:layout_weight="1"
       />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <Button android:id="@+id/help_button2"
    android:layout_weight="1"
    android:layout_width="180dip"
    android:layout_height="wrap_content"
    android:gravity="left"
        style="@style/HelpButton.Dark"
        android:onClick="onClickHelp"
        android:text="@string/help_title_section2"
        android:drawableTop="@drawable/help_image2"/>
    <TextView  
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/help_text_section2"
    android:padding="8dip"
    android:layout_weight="1"
       />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_alignParentLeft="true">
    <Button android:id="@+id/help_button3"
    android:layout_weight="1"
    android:layout_width="180dip"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
        style="@style/HelpButton.Dark"
        android:onClick="onClickHelp"
        android:text="@string/help_title_section3"
        android:drawableTop="@drawable/help_image3"/>
    <TextView  
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/help_text_section3"
    android:padding="8dip"
    android:layout_weight="1"
       />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="left" >
    <Button android:id="@+id/help_button4"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_width="180dip"
    android:layout_height="wrap_content"
     android:gravity="left"
        style="@style/HelpButton.Dark"
        android:onClick="onClickHelp"
        android:text="@string/help_title_section4"
        android:drawableTop="@drawable/help_image4"/>
    <TextView  
       android:layout_width="wrap_content" 
        android:gravity="left"
       android:layout_height="wrap_content" 
       android:text="@string/help_text_section4"
    android:padding="8dip"
    android:layout_weight="1"
       />
</LinearLayout>

【问题讨论】:

  • 您想将按钮对齐在左侧,将 textview 对齐在右侧还是反之亦然?
  • 我尝试使用您的 XML。您的所有视图都正确对齐到左侧。请把你的问题说清楚......你想让你的观点坚持到左边而不留任何空间吗?
  • 有图片:help_button1,下一个是文字.....文字与中心水平对齐,但我需要help_button1和texview一起对齐左侧!

标签: android xml view-helpers


【解决方案1】:

抱歉,没有完全理解。你想让它们垂直排列吗?那么这只是在线性布局中将方向设置为垂直。我看看这个 xml 现在做了什么。它似乎是按钮,它是屏幕的一半,而 textview 是另一半。你能解释一下你想看什么吗?

 <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="4dip"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="wrap_content">

                <Button android:id="@+id/help_button1"
                        android:layout_weight="1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:onClick="onClickHelp"/>
                <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/help_page_intro"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/hello_world"
                    android:textColor="@color/black"
                    android:padding="2dip"
                    android:layout_weight="1"
                    />
                <View
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>
            </LinearLayout>

        </LinearLayout>

【讨论】:

  • 我希望这个按钮和文本视图对齐到左侧(在每一半部分)
  • 仍然不确定我刚刚添加的是否是您的意思。如果不是,那么一个小图像可能会更好地反映您所看到和想要的。因为对于我们其他人来说,它在基本意义上是左对齐的。