java - Imageview 卡在 android 布局的顶部
问题描述
我放了两个线性布局,在第一个线性布局下我放了“textview”,在第二个线性布局下我放了“imageview”。但问题是图像视图没有移动,我想要中间的骰子图标。每当我尝试拖动该图标时,它都会卡在布局的左上角。
我不知道出了什么问题,因为我是 android 新手。
看看截图 https://i.stack.imgur.com/SJmEY.png
activity_main.xml
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/Logo"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="36sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/five" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.snakes;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
解决方案
您不能在 LinearLayout 中拖动元素。它们按照在布局文件中声明的顺序放置,具体取决于方向设置(水平或垂直)。
在您的情况下,也没有理由有两个这样的线性布局。您可以将两个元素放在第一个元素中,并使用重力属性使图像视图居中(例如重力 =“center_vertical”)。
编辑:我最终自己尝试了,并使用 LinearLayout 中的 RelativeLayout 解决了它。确保 RelativeLayout 与父级的宽度和高度相匹配,然后将 ImageView 居中在 RelativeLayout 内,如下所示:
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/someImage"
android:orientation="vertical"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
推荐阅读
- python - 如何最初打印一个字符串,然后设置一个被选中的位,这样就不会在 python 中再次打印
- python - 使用 keras 预测二项式函数
- django-rest-framework - 如何制作不同的 Django Api Url 路径
- r-markdown - 手动创建表格并自动对其标题进行编号,单词输出
- php - Laravel - 在 Jobs 中使用图像干预。图像ttfbbox错误
- r - readr::type_convert 弄乱了时间列
- linux - 带有 Qt 5.12 QSocketNotifier 的 ZeroMQ 只触发一次
- c# - 尽管用户具有正确的角色,但在控制器方法上使用 Authorize 注释仍显示“访问被拒绝”
- java - 使用 1.HttpURLConnection.setRequestProperty 和 2. 使用 CURL 将 b/w http 标头值设置为 null 有什么区别?
- mongodb - mongodb找到key过大的文档