Set same height as width at a ToggleButton

时光怂恿深爱的人放手 提交于 2019-12-25 00:59:09

问题


I have following Layout, with 7 ToggleButtons in a row. Independently from the resolution, I have always 7 Buttons in the complete horizontal width. Now I want to set the same height as the width but it does not work. I tried following without success ():

    tb_Mo = (ToggleButton) findViewById(R.id.tB_Mo);

    int btnSize=tb_Mo.getWidth();
    tb_Mo.setHeight(btnSize);
      //and so on for the other Buttons...

Second try without success:

    int btnSize=tb_Mo.getLayoutParams().width;
    tb_Mo.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
         //and so on for the other Buttons...

XML:

<ToggleButton
    android:id="@+id/tB_Mo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

    <ToggleButton
    android:id="@+id/tB_Di"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

        <ToggleButton
    android:id="@+id/tB_Mi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/> 
          and so on ....

What can I do?


回答1:


Solution:

   <ToggleButton
    android:id="@+id/tB_Sa"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

And:

    int btnSize=tb_Mo.getWidth();
    tb_Mo.setHeight(btnSize);


来源:https://stackoverflow.com/questions/18922755/set-same-height-as-width-at-a-togglebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!