2013-07-03 32 views
2

我想在我的自定義鍵盤中處理 中EditText的inputType屬性。如何居中對齊自定義鍵盤上的按鍵?

由於我必須顯示幾個按鍵,我想將它們對準鍵盤中心。不幸的是,<Row>元素沒有android:gravity屬性。

我試圖創建類似的東西: Screenshot

我想補充一些其他的鍵,並保持它們隱藏起來的空間將被佔用,但我不認爲這是一個很好的解決方案。

你知道任何方式來居中對齊連續的鍵嗎?

謝謝!

回答

1

一個簡單的解決方案是在佈局中採取按鈕,然後採取佈局的重力.... 我提供了一個通用佈局,它可以在所有佈局上運行。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight=".50" 
     ></RelativeLayout> 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight=".50" 
      android:orientation="vertical" 
     > 

     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight=".25" 
       android:gravity="center" 
      > 

      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 

     </LinearLayout> 
     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight=".25" 
       android:gravity="center" 
      > 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
     </LinearLayout> 
     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight=".25" 
       android:gravity="center" 
      > 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
     </LinearLayout> 
     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight=".25" 
       android:gravity="center" 
      > 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
      <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="myTxt" 
       /> 
     </LinearLayout> 


    </LinearLayout> 


</LinearLayout> 

@Ungureanu Liviu,仍然任何查詢,然後plz問。

+1

+ 1很好的例子 –

+1

這是非常好的主意,使通用 – Alpan