将TextView居中对齐并向左对齐

时间:2018-12-05 14:42:25

标签: android alignment

我在垂直LinearLayout中有3个TextView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/green"
    tools:context=".SplashActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="FirstText"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="Second"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightGray"
            android:gravity="center"
            android:text="ThirdText"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

以上代码的当前输出:

enter image description here

但是我希望文本像这样彼此左对齐:

enter image description here

我该如何实现?

2 个答案:

答案 0 :(得分:1)

您需要有一个顶级父级,以其内容为中心。在其中,您将拥有一个中级父级,该父级将内容左对齐。在其中,您将拥有三个TextView。

完成这项工作的关键是使中级父级仅与其内容一样宽。这样,最大的子项将定义中级父项的总体大小,然后顶级父项可以将它们的组居中。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FirstText"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ThirdText"/>

    </LinearLayout>

</FrameLayout>

enter image description here

答案 1 :(得分:-1)

library(stats)
my_data = data.frame(
    success=c(1, 0, 1, 1, 0, 0, 1, 1, 0, 1),
    col_a=c(5000.0, 10000.0, 11000.0, 20000.0, 20000.0, 25000.0, 30000.0, 30000.0, 40000.0, 41000.0),
    col_b=c('a', 'a', 'b', 'b', NA, 'b', 'c', 'c', NA, 'd'),
    col_c=c(5487.0, 5654.0, NA, 2918, 912.0, NA, 1236.0, 234.0, 2390.0, 21092.0)
)
myFormula <- stats::formula(paste0("success ~ col_a + col_b + col_c"))
fit <- stats::glm(myFormula, data=my_data, family=stats::binomial(link="logit"), na.action=stats::na.exclude) 
summary(fit)

Call:
stats::glm(formula = myFormula, family = stats::binomial(link = "logit"), 
    data = my_data, na.action = stats::na.exclude)

Deviance Residuals: 
 [1]  0  0     0        0  0     0

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept)  7.370e+01  1.026e+06       0        1
col_a       -9.826e-03  3.757e+01       0        1
col_bb       1.474e+02  7.500e+05       0        1
col_bc       2.457e+02  1.334e+06       0        1
col_bd       3.538e+02  2.943e+06       0        1
col_c        3.282e-12  1.849e+02       0        1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 5.4067e+00  on 5  degrees of freedom
Residual deviance: 2.5720e-10  on 0  degrees of freedom
  (4 observations deleted due to missingness)
AIC: 12

Number of Fisher Scoring iterations: 23