2017-02-28 67 views
1

我将主布局划分为4个部分。第一部分是一个图像视图,其中每个图像视图具有layout_weight =“2”以及两个线性布局,其中layout_weight =“1”。在每个线性布局中,都有2个CardViews,水平定向,我想用相等的宽度和高度展示它们。 enter image description here动态更改视图宽度不起作用

我改变每个CardView的宽度象下面这样:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home_page); 

    CardView cardView1 = (CardView) findViewById(R.id.cardView1); 
    ViewGroup.LayoutParams params = cardView1.getLayoutParams(); 

    params.width = params.height; // params.height == -1 ??? 
    cardView1.setLayoutParams(params); 
} 

你知道什么问题?谢谢。

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:weightSum="4"> 

<ImageView 
    android:id="@+id/userImage" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2"/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1" 
    android:weightSum="2"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardView1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     app:cardCornerRadius="4dp" 
     android:elevation="5dp" 
     app:cardElevation="10dp" 
     android:paddingBottom="15dp" 
     android:paddingEnd="15dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingStart="15dp" 
     android:paddingTop="15dp" 
     android:layout_margin="15dp" 
     app:cardBackgroundColor="?attr/colorButtonNormal"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="1"/> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cardView2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:elevation="5dp" 
     card_view:cardCornerRadius="4dp" 
     card_view:cardElevation="10dp" 
     android:paddingBottom="15dp" 
     android:paddingEnd="15dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingStart="15dp" 
     android:paddingTop="15dp" 
     android:layout_margin="15dp" 
     card_view:cardBackgroundColor="?attr/colorButtonNormal"> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="2"/> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1" 
    android:weightSum="2"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardView3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:elevation="5dp" 
     app:cardCornerRadius="4dp" 
     android:background="@color/cardview_dark_background" 
     app:cardBackgroundColor="?attr/colorButtonNormal" 
     app:cardElevation="10dp" 
     android:paddingBottom="15dp" 
     android:paddingEnd="15dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingStart="15dp" 
     android:paddingTop="15dp" 
     android:layout_margin="15dp"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="3"/> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cardView4" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:elevation="5dp" 
     card_view:cardCornerRadius="4dp" 
     card_view:cardElevation="10dp" 
     android:paddingBottom="15dp" 
     android:paddingEnd="15dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingStart="15dp" 
     android:paddingTop="15dp" 
     android:layout_margin="15dp" 
     card_view:cardBackgroundColor="?attr/colorButtonNormal"> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="4"/> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

回答

0

如果你的问题是要显示你的观点为Square即宽度和高度应该是平等的,你可以很容易地操作的视图OnMeasure()方法。
由于您的看法是CardView,我修改,

import android.content.Context; 
import android.support.v7.widget.CardView; 
import android.util.AttributeSet; 

public class SquareCardView extends CardView { 

    public SquareCardView(Context context) { 
     super(context); 
    } 

    public SquareCardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareCardView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     //noinspection SuspiciousNameCombination 
     setMeasuredDimension(width, width); // here comes manipulation 
    } 
} 

大功告成:)定义widthheight将等于width

0

如果你想这种观点是正方形其更好地创建它通过延长针对小部件类象下面这样:

public class SquareView extends View{ 

    public SquareView(Context context) { 
     super(context); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    } 
} 

刚分配视图的宽度,它将以高度形成一个正方形。

1

这是因为该视图尚未大小,试试这个:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final CardView cardView1 = (CardView) findViewById(R.id.cardView1); 

    cardView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      // remove the layout listener 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
       cardView1.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } else { 
       cardView1.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      } 

      final int height = cardView1.getHeight(); 
      if (height >= 0) { 
       // use the linearLayout LayoutParams 
       LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(height, height); 
       cardView1.setLayoutParams(newParams); 
      } 
     } 
    }); 
} 
+0

高度计算正确,但cardView1.setLayoutParams(params);不起作用 –

+0

请检查编辑并让我知道它是否有任何区别 –

1

你正在查看高度年初,借鉴了屏幕之前。更好的办法是让屏幕宽度首先使用观察者这样

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       view.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       view.getWidth(); //width is ready here 
       // assign your image view the same height 
       // divide this width by 2 and assign same height width to your cars views 
      } 
     }); 

您可以按照this螺纹了解更多详情

+0

如何设置view.height? view.setLayoutParam()不起作用 –

0

试试这个,你将了解如何使用的LinearLayout重量。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="2"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <!--Place your image view here--> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:weightSum="2"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="2"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <!--CardView 1st--> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <!--CardView 2nd--> 
      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="2"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <!--CardView 3rd--> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1"> 

       <!--CardView 4th--> 

      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout>