【问题标题】:Alignment in android Linear Layoutandroid线性布局中的对齐
【发布时间】:2013-03-06 13:39:37
【问题描述】:

我正在使用带有 2 个文本视图、4 个按钮、1 个搜索栏、1 个图像视图的线性布局。如果我将这些文本视图、按钮等放在线性布局中,则在 android 手机中对齐很好。虽然我在 android 平板电脑上运行相同的代码,但对齐不正确。为什么这种对齐在平板电脑中不正确。?我已经通过 java 代码创建了文本视图、按钮等。即使我通过 devicewidth/2 设置第二个文本视图的左边距来水平指定两个文本视图,这在 android 手机和平板电脑中存在差异。我需要像下面这样对齐。

 TextView1                            TextView2
 Button1 Button2 Button3 Button4      SeekBar  ImageView

这是我的代码。

 LinearLayout.LayoutParams textViewParams1 = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textViewLayout.setOrientation(LinearLayout.HORIZONTAL); 
    TextView TextView1=new TextView(this);
    TextView1.setText("Text1");
    textViewParams1.gravity=Gravity.CENTER;
    textViewParams1.setMargins(60, 20, 40, 10);
    textViewLayout.addView(chooseColorTextView, textViewParams1);

    LinearLayout.LayoutParams textViewParams2 = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    TextView TextView2=new TextView(this);
    TextView2.setText("Text2");
    int width=getWindowManager().getDefaultDisplay().getWidth();
    textViewParams2.gravity=Gravity.CENTER;     
    textViewParams2.setMargins((width/2), 20, 40, 10);
    textViewLayout.addView(strokeWidthTextView, textViewParams2);

    parentlinearLayout.addView(textViewLayout, textViewLayoutParams);

在下一个线性布局中,我添加了 4 个按钮、搜索栏和图像视图。但面临对齐问题。

【问题讨论】:

  • 尝试发布您想要的结果的图表或图片,让我们看看是否可以提供帮助......但我建议采用 XML 方式

标签: android android-layout alignment


【解决方案1】:

我建议创建复杂的布局,该布局必须在 XML 中呈现在不同的屏幕尺寸上,而不是以编程方式呈现,因此您可以在 res/layout 和 res/layout-large 中有两个不同的 main.xml,系统会选择根据屏幕尺寸纠正一个。

【讨论】:

    【解决方案2】:

    在 XML 中使用 layout_weight 和 weightSum:

    <LinearLayout android:weightSum="1" android:layout_width="match_parent" android:layout_height="wrap_content">
        <!-- First column -->
        <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>
    
        <!-- Second column -->
        <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>
    </LinearLayout>
    

    这将产生一个动态调整大小的 2 列布局。如果您希望分割更短或更长,请将 .5 更改为 .3 和 0.7 以获得 30/70% 的分割等。

    【讨论】:

      【解决方案3】:

      请阅读有关 wrap_content 和其他 android 控件的更多信息。另请阅读有关平板电脑的 dpi 的信息。由于分辨率外观变化。

      【讨论】:

        【解决方案4】:

        为什么不使用 TableLayout ?它是您管理单元对齐所需的:

        您只需要在 span 属性中使单元格使用多列。

        TableLayout myLayout = new TableLayout(myContext);
        
        TableRow row1 = new TableRow(myContext);
        TableRow.LayoutParams layouparams1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
        layouparams1.span = 4;
        row1.setLayoutParams = layouparams1 ;
        
        TableRow.LayoutParams layouparams2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
        layouparams2.span = 2;
        TableRow row2 = new TableRow(myContext);
        row2.setLayoutParams = layouparams2 ;
        
        
        //then create and add your childs to each row :
        ...
        row1.addView(text1);
        row1.addView(text1);
        
        row1.addView(button1);
        row1.addView(button2);
        row1.addView(button3);
        row1.addView(button4);
        row1.addView(seekbar);
        row1.addView(imageView);
        
        myLayout.addView(row1);
        myLayout.addView(row2);
        

        还可以考虑为孩子添加 layout_weight 以管理他们留给彼此的空间。

        【讨论】:

          猜你喜欢
          • 2018-03-27
          • 2014-10-24
          • 2011-12-02
          • 1970-01-01
          • 1970-01-01
          • 2014-01-05
          • 1970-01-01
          • 2020-06-28
          • 1970-01-01
          相关资源
          最近更新 更多