2017-03-01 82 views
-1

我正在开发一个项目,我的挑战是让ImageButtons呈方形。我使用的是LinearLayouts,因此我使用layout_weight表示宽度,以便屏幕的宽度可以在ImageButtons"100dp"的预设layout_height之间平均分配。我想现在以编程方式将layout_height更改为分配的layout_width。我查了其他问题,但没有做我想要的。请帮忙。 这里是activity_main.xml文件以编程方式调整图像按钮的大小以保持方形

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.abdul.sdgschampoins.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal1" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal1" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal2" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal2" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal3" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal3" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal4" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal4" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal5" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal5" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal6" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal6" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal7" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal7" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal8" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal8" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal9" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal9" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal10" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal10" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal11" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal11" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal12" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal12" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/layout5" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal13" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal13" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal14" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal14" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal15" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal15" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout6" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageButton 
       android:id="@+id/goal16" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal16" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/goal17" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/goal17" 
       android:layout_margin="4dp"/> 
      <ImageButton 
       android:id="@+id/dummyIcon" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="105dp" 
       android:background="@drawable/sdglogo" 
       android:layout_margin="4dp"/> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

回答

0

我想你应该尝试这样的事:

button.setLayoutParams(new LayoutParams(button.getWidth(), button.getWidth()); 
+0

我想,和'ImageButton'我想它从屏幕上消失。 –

相关问题