Tôi đang cố gắng thực hiện hoạt động nhập mã PIN, nhưng một điều nhỏ không hoàn toàn phù hợp với tôi. Tôi không thể nhận được văn bản đến trung tâm trong TextView tại phía trên cùng của màn hình ở đây:Văn bản Android TextView sẽ không tập trung
Cách nó sẽ làm việc được khi người dùng nhập mã PIN, tôi sẽ đặt một ngôi sao ở mỗi TextView cho phản hồi có thể nhìn thấy. Vấn đề là, tôi muốn ngôi sao được căn giữa. Tôi đã thử layout_gravity = "center", nhưng nó không tạo sự khác biệt. Đây là bố cục hiện tại của tôi:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="PIN Required"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="12sp"
></TextView>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:layout_height="wrap_content"
android:layout_marginBottom="12sp"
android:layout_gravity="center_horizontal"
>
<TextView
android:id="@+id/text1"
android:layout_height="32sp"
android:layout_width="48sp"
android:layout_weight="1"
android:layout_marginRight="2sp"
android:layout_marginLeft="48sp"
android:background="#FFFFFF"
android:text="X"
android:layout_gravity="center"
></TextView>
<TextView
android:id="@+id/text2"
android:layout_height="32sp"
android:layout_width="48sp"
android:layout_weight="1"
android:layout_marginRight="2sp"
android:layout_marginLeft="2sp"
android:background="#FFFFFF"
></TextView>
<TextView
android:id="@+id/text3"
android:layout_height="32sp"
android:layout_width="48sp"
android:layout_weight="1"
android:layout_marginRight="2sp"
android:layout_marginLeft="2sp"
android:background="#FFFFFF"
></TextView>
<TextView
android:id="@+id/text4"
android:layout_height="32sp"
android:layout_width="48sp"
android:layout_weight="1"
android:layout_marginRight="48sp"
android:layout_marginLeft="2sp"
android:background="#FFFFFF"
></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout2"
android:layout_height="wrap_content"
android:layout_marginBottom="12sp"
>
<Button
android:text="1"
android:textColor="#FFFFFF"
android:id="@+id/button1"
android:layout_height="32sp"
android:layout_width="64sp"
android:layout_weight="1"
android:editable="false"
android:layout_marginRight="2sp"
android:layout_marginLeft="48sp"
android:background="@drawable/action_background_gradient"
android:onClick="ButtonClicked"
></Button>
<Button
android:text="2"
android:textColor="#FFFFFF"
android:id="@+id/button2"
android:layout_height="32sp"
android:layout_width="64sp"
android:layout_weight="1"
android:editable="false"
android:layout_marginRight="2sp"
android:layout_marginLeft="2sp"
android:background="@drawable/action_background_gradient"
android:onClick="ButtonClicked"
></Button>
<Button
android:text="3"
android:textColor="#FFFFFF"
android:id="@+id/button3"
android:layout_height="32sp"
android:layout_width="64sp"
android:layout_weight="1"
android:editable="false"
android:layout_marginRight="48sp"
android:layout_marginLeft="2sp"
android:background="@drawable/action_background_gradient"
android:onClick="ButtonClicked"
></Button>
</LinearLayout>
<!--... and so on for the rest of the buttons ... -->
</LinearLayout>
Tôi có android: text = "X" trong đó cho lần xem văn bản đầu tiên để tôi có thể xem nó có đang hoạt động hay không. Khi tôi thực hiện tinh chỉnh bố cục, nó sẽ biến mất. Tôi thiếu điều gì đơn giản?
Đó là bởi vì tôi là một knucklehead và tôi có nghĩa là để nói TextView. Làm việc trên hai thứ cùng một lúc. Nhưng tôi có lực hấp dẫn = "trung tâm" ở đó và nó không hoạt động. – MrGibbage
Doh! Tôi có layout_gravity, chứ không chỉ là lực hấp dẫn. Sigh ... Cảm ơn! – MrGibbage
Cảm ơn, điều này giúp tôi rất nhiều. – Kedarnath