java - 如何使按钮方形和可调整大小

标签 java android android-layout android-button layoutparams

我想把按钮(两个其他按钮之间的那个)做成正方形。

enter image description here

我尝试使用以下代码:

toggleButton = (ToggleButton) findViewById(R.id.activity_dictionary_toggleButton1);
LinearLayout.LayoutParams toggleButtonLayoutParams = (LayoutParams) toggleButton.getLayoutParams(); 
toggleButtonLayoutParams.width = toggleButtonLayoutParams.height;
toggleButton.setLayoutParams(toggleButtonLayoutParams);

但它不起作用(按钮看起来像上图)。这是我的 XML 文件的一部分:

<LinearLayout
    android:id="@+id/activity_dictionary_linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="5dp" >

    <Button
        android:id="@+id/activity_dictionary_button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:enabled="false"
        android:maxWidth="200dp"
        android:text="Clear"
        android:textColor="@android:color/white" />

    <ToggleButton
        android:id="@+id/activity_dictionary_toggleButton1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.2"
        android:background="@drawable/settings"
        android:text="ToggleButton" />

    <Button
        android:id="@+id/activity_dictionary_button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:enabled="false"
        android:maxWidth="200dp"
        android:text="Recognize"
        android:textColor="@android:color/white" />

</LinearLayout>

我应该改变什么?

更新:android:layout_weight="0.2" 被删除了,但是当我尝试通过代码实现它时 Button 仍然没有改变它的大小(它是圆的但非常小)。

最佳答案

您应该将 LinearLayout 切换为 RelativeLayout。您的尺寸参数应该会生效。

关于java - 如何使按钮方形和可调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17078592/

相关文章:

java - 让 javadoc 与 netbeans 一起工作

java - MySQL - 希伯来字符成为数据库中的问号

android - 无需root手机即可设置WiFi代理

android - 在 Activity 可见后如何运行我的代码?

android - EditText 不显示当前输入 (Android 4)

android - 是否可以为 textview 的复合绘图提供状态?

Android:RecyclerView 滚动问题

Java 和数据库 : on using multiple ResultSets and Cursors

java - 当在 Spring Boot 应用程序上禁用 open-in-view 时,Eclipse 或 Spring Boot 可以帮助我找到需要事务的方法吗?

Android Gallery 图像之间的过渡