【问题标题】:Android Image View does not start from topAndroid Image View 不是从顶部开始的
【发布时间】:2018-11-13 03:25:53
【问题描述】:

我有两个并排放置的线性布局。右侧布局包含一个图像条。问题是图像不是从顶部开始的,还会出现不需要的底部边距,从而产生不必要的滚动。 最好是完全没有不必要的滚动。左侧还有更多按钮。右侧图像可能会被剪裁。

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="2"
                android:orientation="vertical" >

            <TextView
                android:id="@+id/textViewCaption"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="This is my Application"
                android:textSize="40sp" />

            <Button
                android:id="@+id/btnOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button One"
                android:textSize="@dimen/home_sub_caption_height" />

            <Button
                android:id="@+id/btnTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button Two"
                android:textSize="25sp" />

            <Button
                android:id="@+id/btnThree"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button Three"
                android:textSize="25sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/imageViewLogo"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/qutub" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 更改启示图片
  • 250*250 取那个尺寸
  • 如果不需要滚动,只需使用LinearLayout

标签: android android-layout layout


【解决方案1】:

在ImageView中使用 android:layout_height="wrap_content"

而不是 android:layout_height="match_parent"

我想这对你有用。

【讨论】:

    【解决方案2】:

    如果您不需要滚动,请移除 ScrollView

    <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:layout_weight="2"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/textViewCaption"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="This is my Application"
                android:textSize="40sp" />
    
            <Button
                android:id="@+id/btnOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button One" />
    
            <Button
                android:id="@+id/btnTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button Two"
                android:textSize="25sp" />
    
            <Button
                android:id="@+id/btnThree"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00FFFFFF"
                android:text="Button Three"
                android:textSize="25sp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:layout_weight="1">
    
            <ImageView
                android:id="@+id/imageViewLogo"
                android:layout_width="match_parent"
                android:scaleType="fitXY"
                android:layout_height="match_parent"
                android:src="@drawable/profile" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
      
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_margin="10dp"
              android:layout_weight="2"
              android:orientation="vertical" >
      
              <TextView
                  android:id="@+id/textViewCaption"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:gravity="left"
                  android:text="This is my Application"
                  android:textSize="40sp" />
      
              <Button
                  android:id="@+id/btnOne"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#00FFFFFF"
                  android:text="Button One"
                 />
      
              <Button
                  android:id="@+id/btnTwo"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#00FFFFFF"
                  android:text="Button Two"
                  android:textSize="25sp" />
      
              <Button
                  android:id="@+id/btnThree"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="#00FFFFFF"
                  android:text="Button Three"
                  android:textSize="25sp" />
          </LinearLayout>
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_margin="0dp"
              android:layout_weight="1">
              <ImageView
                  android:id="@+id/imageViewLogo"
                  android:layout_width="250dp"
                  android:layout_height="250dp"
                  android:layout_weight="1"
                  android:src="@drawable/profile" />
          </LinearLayout>
        </LinearLayout>
       </ScrollView>
      

      【讨论】:

        【解决方案4】:

        ImageView中添加这个属性

        android:scaleType="fitStart"

        希望这会有所帮助!

        【讨论】:

        • 如果我这样做,图像顶部对齐,但底部边距加倍。图片下方有很多间隙,有很多不必要的滚动。
        • 你能给我看一下动画版的布局示例吗?
        【解决方案5】:

        尝试设置Imageviewlayout_gravity="top"layout_height="wrap_content"

        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="3">
        
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical">
        
                <TextView
                    android:id="@+id/textViewCaption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:text="This is my Application"
                    android:textSize="40sp" />
        
                <Button
                    android:id="@+id/btnOne"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#00FFFFFF"
                    android:text="Button One"
                    android:textSize="25sp" />
        
                <Button
                    android:id="@+id/btnTwo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#00FFFFFF"
                    android:text="Button Two"
                    android:textSize="25sp" />
        
                <Button
                    android:id="@+id/btnThree"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#00FFFFFF"
                    android:text="Button Three"
                    android:textSize="25sp" />
            </LinearLayout>
        
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
        
                <ImageView
                    android:id="@+id/imageViewLogo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/qutub"
                    android:layout_gravity="top"/>
            </LinearLayout>
        </LinearLayout>
        </ScrollView>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-14
          • 2016-05-28
          • 1970-01-01
          相关资源
          最近更新 更多