【问题标题】:Eclipse Android Layout: Without dp-values?Eclipse Android 布局:没有 dp 值?
【发布时间】:2015-09-05 21:41:49
【问题描述】:

如果我想在布局的中心放置一张图片,我很容易使用:“垂直居中”和“水平居中”。但现在我想要一张图片将它放在左侧的中心。但我不想使用 marginLeft=..dp。我想在没有 dp 的情况下工作。有没有像“垂直居中”之类的可能性?这是带有 dp 值的代码。我可以用什么来代替 MarginLeft"38dp"?

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
        <ImageView
    android:id="@+id/imageView1"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerInParent="true"
    android:layout_marginLeft="41dp"
    android:src="@drawable/rosezwei" />

</RelativeLayout>

【问题讨论】:

  • android:layout_centerInParent="true"?
  • 你的意思是像上面编辑过的帖子?
  • 是的。那不行吗?
  • 不,图片停留在布局的中心...
  • 抱歉,问题看错了。也许更准确地解释你想要什么。您不希望它水平居中,并且您不想使用“dp”,无论这意味着什么,那么您想要什么 do 呢?我的意思是,如果您不想要边距,就不要使用边距。

标签: android eclipse layout


【解决方案1】:

使用

 android:layout_gravity="left|center_vertical"

将在 LinearLayout 视图中工作。

<?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" >
    <ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="left|center_vertical"
android:src="@drawable/rosezwei" />

</LinearLayout >

注意,LinearLayout 是水平的,所以视图可以选择它的垂直位置。

【讨论】:

  • 我通过你的代码替换了 "android:layout_centerInParent="true"" 和 "android:layout_marginLeft="41dp" ,但没有帮助。还是让我错了?
  • 嗯,这就是解决方案,也许相对布局不接受重力参数,您可以尝试使用 FrameLayout 代替吗?
  • 不,那行不通。图片不在左侧的中心,而是直接在边缘。
  • 它在 0,0(左上角)还是左中?
  • 如果有帮助请看我的回答
【解决方案2】:

如果我理解正确,您希望图像中心位于屏幕宽度的 25%...
唉 Android 在 XML 中没有百分比,要做到这一点,您必须使用 LinearLayout 来拆分屏幕

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <ImageView
      android:id="@+id/imageView1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight=".5"
      android:layout_gravity="center"
      android:src="@drawable/rosezwei" />
   <Space
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight=".5"/>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多