【问题标题】:Android Designing with LinearLayout , how it works?使用 LinearLayout 进行 Android 设计,它是如何工作的?
【发布时间】:2018-08-07 09:10:53
【问题描述】:

我正在为一个简单的页面使用 Android LinearLayout,我从一个 AXML 文件中为纵向和横向模式添加了 1 个 ImageView 和 1 个 GridView。

我希望在横向模式下,所有项目都排成一行,而在纵向模式下,gridvew 将减少到第二行。

我该如何解决这个问题。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">  
  <ImageView
           android:id="@+id/imageView2"
           android:layout_width="300dp"
           android:layout_height="100dp"           
           android:src="@drawable/TESTIMAGEGOF"
           android:layout_marginBottom="1dp" />
    <GridView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:columnWidth="75dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:stretchMode="spacingWidthUniform"
        android:id="@+id/gridView1" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    要在一个布局中设置,你必须使用java代码来解决这个问题,你应该做2步:

    1- 您必须使用 java 检查设备中的方向:

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    
    }
    

    2- 要在您的 LinearLayout 中设置方向,请尝试以下代码:

    LinearLayout layout = /* ... */;
    layout.setOrientation(LinearLayout.VERTICAL);
    

    你可以使用这种技术。

    【讨论】:

    • 感谢@Mohammad Akbari 的回复。我们正在开发 Xmarine c# 项目。所以java代码不起作用。我们根据您的参考尝试使用相同的 C# 代码,但它不起作用。线性布局 lin = new LinearLayout(this); lin.Orientation = Orientation.Horizo​​ntal;
    【解决方案2】:

    您必须在 res 文件夹中创建 2 文件夹,名称为:layoutlayout-land 并在这 2 个文件夹中创建具有相同名称的 xml 文件,对于 layout 使用以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">  
      <ImageView
               android:id="@+id/imageView2"
               android:layout_width="300dp"
               android:layout_height="100dp"           
               android:src="@drawable/TESTIMAGEGOF"
               android:layout_marginBottom="1dp" />
        <GridView
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:columnWidth="75dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="2dp"
            android:horizontalSpacing="2dp"
            android:stretchMode="spacingWidthUniform"
            android:id="@+id/gridView1" />
    
    </LinearLayout>

    并在 layout-land 文件夹文件中使用以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">  
      <ImageView
               android:id="@+id/imageView2"
               android:layout_width="100dp"
               android:layout_height="100dp"           
               android:src="@drawable/TESTIMAGEGOF"
               android:layout_marginBottom="1dp" />
        <GridView
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:columnWidth="75dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="2dp"
            android:horizontalSpacing="2dp"
            android:stretchMode="spacingWidthUniform"
            android:id="@+id/gridView1" />
    
    </LinearLayout>

    【讨论】:

    • 感谢@Mohammad Akbari 的回复。我之前试过这个,但问题是我们可以通过设置任何属性或以任何其他方式在一个布局中做同样的事情吗?
    【解决方案3】:

    哦...你在 xamarin 工作。

    在 xamarin 中检查方向是否垂直:

    private double width = 0;
    private double height = 0;
    
    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height); //must be called
        if (this.width != width || this.height != height)
        {
            this.width = width;
            this.height = height;
    
            if(width > height){
                //device is landscape
            }
        }
    }
    

    在 LinearLayout 中设置方向使用:

    LinearLayout LLMain = new LinearLayout(this);
    LLMain.Orientation = Orientation.Horizontal;
    

    【讨论】:

    • 这里我从 AXML 页面中删除了方向。但不工作。代码是int LayoutOrientation = WindowManager.DefaultDisplay.Orientation; if (LayoutOrientation != 0) { // landscape view } else { // Portrait view LinearLayout lin = new LinearLayout(this); lin.Orientation = Orientation.Vertical; }
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多