我找到了一个答案,为什么即使调用 requestLayout() 权重也不会改变。我制作了一个类似于 karokyo 的程序,但我的不使用 XML 布局文件。就像上面的 karakyo 一样,它使用 seekBar 交互地改变权重。但它以编程方式创建所有视图。我发现在将视图添加到 ViewGroup(例如,LinearLayout)时,提供给 addView() 的 LayoutParams 不会复制到视图中,而是作为对象添加。因此,如果在多个 addView() 调用中提供了一个 LayoutParams,那么添加的视图将共享相同的 LayoutParams。然后,如果一个 View 的权重发生变化(通过分配给 getLayoutParams()).weight),其他 View 的权重也会发生变化,因为它们共享相同的 LayoutParams 对象。显然,当从 XML 布局膨胀视图时,
package course.example.interactiveweightexperiment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class WeightSeekBar extends Activity {
private final String TAG = "WeightSeekBar-TAG";
private LinearLayout mainLayout;
private LinearLayout linearColors;
private View redView;
private View blueView;
SeekBar weightSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
redView = new View(this);
redView.setBackgroundColor(0xff770000);
blueView = new View(this);
blueView.setBackgroundColor(0xff000077);
weightSeekBar = new SeekBar(this);
weightSeekBar.setMax(100);
weightSeekBar.setProgress(50);
weightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
((LinearLayout.LayoutParams) redView.getLayoutParams()).weight = 100 - progress;
((LinearLayout.LayoutParams) blueView.getLayoutParams()).weight = progress;
linearColors.requestLayout();
// linearColors.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
linearColors = new LinearLayout(this);
linearColors.setOrientation(LinearLayout.VERTICAL);
linearColors.addView(redView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 50));
linearColors.addView(blueView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 50));
/* the following does not allow the child Views to be assigned different weights.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 50);
linearColors.addView(redView, params);
linearColors.addView(blueView, params);
*/
mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.addView(linearColors, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 19));
mainLayout.addView(weightSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1));
setContentView(mainLayout);
}
}