【问题标题】:Width of LayoutParams is ignored for the outer view when set in java code (instead of xml layout)在 java 代码中设置时,外部视图的 LayoutParams 的宽度被忽略(而不是 xml 布局)
【发布时间】:2010-10-28 16:06:47
【问题描述】:

我创建了一个非常简单的活动,在该活动中,我使用 java 代码而不是 xml 创建和设置视图。我传递给外部 LinearLayout 的宽度虽然完全没有效果(200)。无论我在这里传递什么值,视图都会显示在屏幕的整个宽度上。

(请注意,这只是示例代码;我知道在实际应用中您不会使用固定值。我只是想在此处指出我的问题,以便于说明)。 p>

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // outer linear layout
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(
                200,   // *** this param has no effect, regardless of the value I set here ***
                LinearLayout.LayoutParams.FILL_PARENT
        ));
        ll.setBackgroundColor(Color.parseColor("#00ff00"));


        // inner linear layout
        LinearLayout ll2 = new LinearLayout(this);
        ll2.setLayoutParams(new LinearLayout.LayoutParams(
                100,    // ** this width for the inner view is working fine **
                LinearLayout.LayoutParams.FILL_PARENT
        ));
        ll2.setBackgroundColor(Color.parseColor("#0000ff"));
        ll.addView(ll2);

        setContentView(ll);
    }
}

但如果我替换 setContentView(ll); 并使用 xml 布局而不是外部 LinearLayout 的值为 200px,它会正确应用并且视图只占用屏幕的 200px。

setContentView(com.example.R.layout.main);

main.xml 在哪里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200px"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    >
    <TextView  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
</LinearLayout>

为什么在java代码中为外部布局设置固定宽度没有效果?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我认为这是 Android 中的一个错误,在

    处提交了错误报告

    http://code.google.com/p/android/issues/detail?id=12244

    【讨论】:

    • 查看我的错误报告中的 cmets。这种行为是“按预期工作”,但我建议它可以改进。我需要使用 setContentView(ll, params) 而不仅仅是 setContentView(ll),恕我直言有点不合逻辑。
    【解决方案2】:

    你可以试试这个方法。

      LinearLayout ll = new LinearLayout(this);       
      LinearLayout.LayoutParams lparam = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
      lparam.width = 200;
    
      ll.setLayoutParams(lparam);
      ll.setBackgroundColor(Color.parseColor("#00ff00"));
    
      setContentView(ll);
    

    【讨论】:

      【解决方案3】:

      我不能确切地告诉你'为什么',但也许 xml 有一种“不可见的父级”来提供锚点。如果您迫切需要一种解决方法,那么显而易见的答案是用这样的不可见的外部父级包装它:

          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          // very, very outer Linear layout!
          LinearLayout ll_InvisibleParent = new LinearLayout(this);
          ll_InvisibleParent.setBackgroundColor(Color.parseColor("#000000"));
      
          // outer Linear layout
          LinearLayout ll = new LinearLayout(this);
          //
          ll.setLayoutParams(new LinearLayout.LayoutParams(
                  200,   // *** this param has no effect, regardless of the value I set here ***
                  LinearLayout.LayoutParams.FILL_PARENT
          ));
          ll.setBackgroundColor(Color.parseColor("#00ff00"));// green
      
          // inner Linear layout
          LinearLayout ll2 = new LinearLayout(this);
          ll2.setLayoutParams(new LinearLayout.LayoutParams(
                  100,    //  this width for the inner view is working fine 
                  LinearLayout.LayoutParams.FILL_PARENT
          ));
          ll2.setBackgroundColor(Color.parseColor("#0000ff"));// blue
          ll.addView(ll2);
          //setContentView(ll); REPLACED WITH INVISIBLE PARENT
          ll_InvisibleParent.addView(ll);
          setContentView(ll_InvisibleParent);
      }
      

      对我有用

      【讨论】:

      • 感谢您的回复,我并不渴望解决方法,我已经有了解决方法。只是想知道为什么参数不起作用。关于:'invisible parent' 提供锚点——这应该是 DecorView 的工作,而不是外部 LinearLayout。外层 LinearLayout 应该和 xml 布局中的 llOuter 一样。
      • 但在我的情况下,由于我的特殊情况,您的解决方法不起作用:我有一个 ActivityGroup,它的 LinearLayout 视图组的宽度是屏幕宽度的 3 倍(因为它应该是可滚动的)。在这个活动组中,我嵌入了三个活动(它是 DecorViews)。使用上面的 invisibleParent,我认为它会占用太多空间。顺便说一句:如果您没有明确设置,那么 ll_InvisibleParent 的默认宽度是多少。
      • 不确定默认值可能是什么,但我确定您提出错误是正确的,请参阅android.git.kernel.org/?p=platform/frameworks/… 它在 1.5 下崩溃,并且 LinearLayout 的 mBaselineAlignedChildIndex 设置为超出范围的索引. 1.5中为0,2.1下为-1
      【解决方案4】:

      我使用以下解决方案解决了这个问题。您必须在 view's 父布局上调用 getLayoutParams。但是您的最外层布局将没有任何父级。如果你想为你的最外层布局设置layoutparams,你应该在ViewGroup上调用geLayoutParams

       LinearLayout ll = new LinearLayout(this);
          ll.setLayoutParams(new ViewGroup.LayoutParams(
                  200,   // *** this param has no effect, regardless of the value I set here ***
                  ViewGroup.LayoutParams.FILL_PARENT
          ));
      

      【讨论】:

      • @Mathias Lin 当你很久以前问过这个问题时,请给我你对我的回答的反馈,这是做这件事的好方法。我遇到了同样的问题,我就这样解决了
      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      相关资源
      最近更新 更多