如何以编程方式在linearlayout中设置cardview的布局宽度?

时间:2017-10-24 06:00:57

标签: android android-layout android-cardview

我正在尝试在MATCH_PARENT和0dp之间更改两个cardview元素的布局宽度。当我通过xml更改模拟场景时,它是正确的,但是,当我以编程方式执行时,它不会按预期呈现。

XML

//as-is scenario; cardSectionA and cardSectionB should be shown
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        cardSectionA.getLayoutParams().width,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

cardSectionA.setLayoutParams(params);
cardSectionB.setLayoutParams(params);   

//should only show cardSectionA scenario
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

cardSectionA.setLayoutParams(params);

java

compute_weight_by_theta = function(X,Y,xtrain,ytrain,weights.radial){
      N = length(xtrain)
      weight.directional = integer(N)
      for (i in 1:N){
        numerator = 0;
        denominator = 0;
        for (j in 1:N){
          xi = xtrain[i]; yi = ytrain[i]
          xj = xtrain[j]; yj = ytrain[j]
          if ((xi != xj) | (yi != yj)){
            Sj = weights.radial[j]
            Di = sqrt((X - xi)**2 + (Y - yi)**2)
            Dj = sqrt((X - xj)**2 + (Y - yj)**2)
            cos_theta = ((X-xi)*(X-xj) + (Y-yi)*(Y-yj)) / (Di*Dj)
            numerator = numerator + (1-cos_theta)*Sj
            denominator = denominator + Sj
          }
        }
        weight.directional[i] = numerator/denominator
      }
      return(weight.directional)
    }

非常感谢。

1 个答案:

答案 0 :(得分:0)

想出来。为了按预期运行,还必须设置边距和布局权重。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

int margin = 10;
params.setMargins(margin, margin, margin, margin);
params.weight = 1;


cardSectionA.setLayoutParams(params);