2017-10-13 79 views
0

如何在3 * 4(l * b)佈局中製作圓形按鈕?網格佈局中的圓形按鈕4 * 3佈局

我試過了一個代碼,它顯示了Android Studio中的圓形按鈕佈局。但是,當它在手機上運行時,尺寸會被壓縮。如何解決這個問題?

<?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" 
    android:weightSum="4" 
    android:background="@drawable/bg"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:weightSum="3" > 

     <Button 
      android:id="@+id/loadmoney" 
      android:text="Load\nMoney" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" 
      android:drawableTop="@android:drawable/ic_menu_edit"/> 

     <Button 
      android:id="@+id/requestmoney" 
      android:text="Request\nMoney" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/sendmoney" 
      android:text="Send\nMoney" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:weightSum="3" > 

     <Button 
      android:id="@+id/recharge" 
      android:text="Recharge" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/paybill" 
      android:text="Pay Bill" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/checkbalance" 
      android:text="Check\nBalance" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:weightSum="3" > 

     <Button 
      android:id="@+id/cashout" 
      android:text="CAsh Out" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/paymerchant" 
      android:text="Pay\nMerchant" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/viewtransaction" 
      android:text="View\nTransaction" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginBottom="10dp" 
     android:weightSum="3" > 

     <Button 
      android:id="@+id/loan" 
      android:text="Loan" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/insurance" 
      android:text="Insurance" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

     <Button 
      android:id="@+id/kyc" 
      android:text="Know Your\nCustomer" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/dashboard_button" /> 

    </LinearLayout> 

</LinearLayout> 

此代碼給出了確切的圓形視圖。但它被壓縮並運行

+0

嘗試創建圓形繪製https://stackoverflow.com/questions/10266595/how-to-make-a-round-button而不是使用多個按鈕去與網格視圖 – Akshay

回答

0

根據你的代碼,將按鈕的寬度=屏幕寬度/ 4,身高= screenHeight/4

除非你screenHeight後在手機上觀看時的圓形按鈕的大小受到干擾等於screenHeight,否則會毫無疑問地被壓縮;

+0

您可以更改按鈕的屬性動態你的活動。 – Deng

+0

int screenWidth = getWindowManager()。getDefaultDisplay()。getWidth(); int screenHeight = getWindowManager()。getDefaultDisplay()。getHeight(); screenWidth = screenWidth Deng