【问题标题】:Center view in android after changing its width properties更改宽度属性后在android中的中心视图
【发布时间】:2014-03-26 17:36:49
【问题描述】:

我正在根据其列宽和列数(有效)更改网格视图的宽度:

gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);

我已经尝试在父视图中重新居中,但它不起作用,有谁知道为什么,它仍然居中,好像在我的动态更改之前它仍然具有宽度参数。

【问题讨论】:

    标签: android android-layout gridview android-linearlayout gravity


    【解决方案1】:

    Gravity 设置为LayoutParams 属性,然后将LayoutParams 设置为您的GridView,如下所示...

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
    params.gravity = Gravity.CENTER;
    
    gridView.setLayoutParams(params);
    

    【讨论】:

    • 它还在左上角!
    • 尝试使用Gravity.CENTER_VERTICAL 而不是Gravity.CENTER
    【解决方案2】:

    如果它比它的父级大,你不能居中,它永远不会比屏幕大,除非你手动设置它,这将导致系统在屏幕外渲染数据并减慢你的速度.....

    它是垂直扩展还是水平扩展,还是两者兼而有之?我的建议是将它放在滚动视图中。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 2019-08-11
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多