【问题标题】:Divide screen in two half and place imageview on half-half both将屏幕分成两半并将图像视图放在两半上
【发布时间】:2016-02-26 06:34:17
【问题描述】:

我正在设计一个页面,但在这一点上卡住了。如果有人知道请告诉。 我想这样做:

放置像鸟一样的圆角方形图像。

【问题讨论】:

    标签: android android-layout android-imageview android-relativelayout


    【解决方案1】:

    自从我 2 年前发布此帖子以来,我看不到任何有用的答案,因此我自己回答。

    可以在CoordinatorLayout 中使用layout_anchor 和layout_anchorGravity 来完成,我们可以将View 锚定到另一个View。它非常简单,因为它是 CoordinatorLayout 的内置行为。

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MyActivity">
    
        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/img_banner" />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_logo"
            app:layout_anchor="@id/header"
            app:layout_anchorGravity="bottom|center_horizontal" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案2】:
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.example.testing.MainActivity" >
      
      <LinearLayout 
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
      
          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:background="#0000ff"
              android:orientation="vertical"
              android:layout_weight="1" />
      
          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:background="#00ffff"
              android:orientation="vertical"
              android:layout_weight="1" />
      
      </LinearLayout>
      
      <LinearLayout 
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:orientation="vertical"
          android:background="#00ff00"
          android:layout_gravity="center" />
      
      </FrameLayout>
      

      看起来像这样

      【讨论】:

      • 下部应根据布局内容在高度上可伸缩
      • 你可以在较低的线性布局下使用滚动视图
      • 但是上半部分也应该跟着上移。
      • 那么你应该使用 coodinator 布局。以下链接可能对您有所帮助。 guides.codepath.com/android/…
      • 我想,就是这样。
      【解决方案3】:

      您可以在这里找到解决方案:

      https://stackoverflow.com/a/25143739/5907003

      如果你想calculate the height dynamically你可以使用下面的代码:

      int finalHeight, finalWidth;
      final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
      final TextView tv = (TextView)findViewById(R.id.size_label);
      ViewTreeObserver vto = iv.getViewTreeObserver();
      vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
          public boolean onPreDraw() {
              iv.getViewTreeObserver().removeOnPreDrawListener(this);
              finalHeight = iv.getMeasuredHeight();
              finalWidth = iv.getMeasuredWidth();
              tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
              return true;
          }
      });
      

      如果可以提供帮助,也只是一个想法,您可以将图像分成两部分并将它们与您的布局对齐,如 align Bottom 和 align top 与各自的布局

      对于圆角背景,您可以在可绘制文件夹中创建一个 bg.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <solid android:color="#FFFFFF"/>
          <stroke android:width="3dip" android:color="#B1BCBE" />
          <corners android:radius="10dip"/>
          <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
      </shape>
      

      祝你好运!

      【讨论】:

        【解决方案4】:

        您可以使用布局组件Guideline 以像素或百分比分割屏幕,并将其用作锚点来固定任何其他组件。

        【讨论】:

          猜你喜欢
          • 2020-07-20
          • 2016-03-27
          • 1970-01-01
          • 2015-11-27
          • 2023-02-17
          • 2022-06-12
          • 1970-01-01
          • 2022-10-25
          • 2013-05-25
          相关资源
          最近更新 更多