【问题标题】:Android: layout -> button selector -> button not getting smallerAndroid:布局->按钮选择器->按钮没有变小
【发布时间】:2011-08-29 16:27:41
【问题描述】:

所以我有这个问题。我制作了一个按钮和一个 .xml 按钮选择器文件,除了一件事,一切都很好。

我想要发生的事情:当您单击(或选择)按钮时,按钮会变小(准确地说,它会变为较小的 .png 文件)。

发生了什么:当您单击(或选择)按钮时,按钮并没有变小,而是被拉长以适合父级(总而言之,大小保持不变)。我认为这与错误的布局参数有关。你能帮帮我吗?这是一些代码:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/listbg"
    android:paddingTop="110dip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal">
    <ScrollView 
        android:id="@+id/scrollView1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content">
    <RelativeLayout 
        android:id="@+id/linearLayout1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal">
        <Button android:layout_height="wrap_content"
            android:id="@+id/Button1" 
            android:background="@drawable/placestovisit"
            android:state_pressed="true"
            android:layout_width="wrap_content" 
            android:layout_alignParentTop="true"/>
        <!-- This is the button I am talking about -->
        <Button android:layout_height="wrap_content"
            android:id="@+id/Button2" 
            android:background="@drawable/mostpopular_button"
            android:layout_width="wrap_content" 
            android:layout_below="@+id/Button1"/>

【问题讨论】:

    标签: android button size selector


    【解决方案1】:

    将此添加到所有按钮:

         android:onClick="doButtonAction"
    

    在您的活动中为按钮布局执行此操作

    public void doButtonAction(View view)
    {
       Button yourButton = (Button)findViewById(view.getId());
       yourButton.setScaleX(.8);
       yourButton.setScaleY(.8);
       //do button stuff here
    }
    

    如果这本身不起作用,可以尝试显式设置按钮的高度和宽度,例如:

         android:layout_height="50px"
         android:layout_width="150px"
    

    避免使用 Wrap Content 可能会阻止拉伸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多