我在RelativeLayout中有一个按钮中心,按钮下方有两个图像。 当我将RelativeLayout包装在ScrollView中时,我有两个问题:
按钮现在位于包括可滚动部分的relativelayout中心。这不是我想要的,我想让它保持在屏幕中我看不到滚动的部分。
我的centerInParent按钮下方的图片不再位于按钮下方。它就像按钮位于左上角一样。
因此,图像位于顶部的白色空间下方,就像按钮在那里一样,按钮在我的布局中居中但不是我想要的:D
这是我的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:background="#E74E4C"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
<ImageView
android:id="@+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@id/button"
android:layout_margin="16dp"
android:scaleType="centerCrop"
android:src="@drawable/myimage" />
<ImageView
android:id="@+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@id/imageone"
android:layout_margin="16dp"
android:scaleType="centerCrop"
android:src="@drawable/myimagetwo" />
</RelativeLayout>
</ScrollView>
你看到了什么问题吗?有关信息,我打算添加一个底部导航栏。
答案 0 :(得分:1)
以编程方式根据比例给出topMargin,你就完成了!如果你想调整你可以改变值无关紧要显示哪个屏幕显示相同的比率。
这是我的xml与你的略有不同 - (更改ID我只是添加了一些)
private int screenHeight;
private int screenWidth;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
relativeLayout =(RelativeLayout) findViewById(R.id.lin);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenHeight = size.y;
screenWidth = size.x;
LinearLayout.LayoutParams relBtn = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relBtn.topMargin = ((1920/2) *screenHeight/1920); // ratio based mine height is 1920 so to get it to middle i give half height, will be same in your mobiles too , you can change 1920/2 as you like
relativeLayout.setLayoutParams(relBtn);
}
代码
RelativeLayout.LayoutParams
与<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HELLO"
android:textColor="#FFF"
android:textSize="20dp" />
<RelativeLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:background="@color/colorPrimaryDark"
android:gravity="bottom"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</RelativeLayout>
<ImageView
android:id="@+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/lin"
android:layout_margin="16dp"
android:background="@drawable/girl"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/imageone"
android:layout_margin="16dp"
android:background="@drawable/amanda"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
相同的事情
的xml:
private int screenHeight;
private int screenWidth;
private TextView textView;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
relativeLayout =(RelativeLayout) findViewById(R.id.lin);
textView =(TextView) findViewById(R.id.tv);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenHeight = size.y;
screenWidth = size.x;
RelativeLayout.LayoutParams tv = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv.topMargin = ((100)*screenHeight/1920); // ratio based mine height is 1920 so to get it to middle i give half height, will be same in your mobiles too , you can change 1920/2 as you like
textView.setLayoutParams(tv);
RelativeLayout.LayoutParams relBtn = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relBtn.addRule(RelativeLayout.CENTER_HORIZONTAL);
relBtn.topMargin = ((1920/2 - 100 ) *screenHeight/1920); // ratio based mine height is 1920 so to get it to middle i give half height, will be same in your mobiles too , you can change 1920/2 as you like
relativeLayout.setLayoutParams(relBtn);
}
代码:
{{1}}
输出:
答案 1 :(得分:0)
答案 2 :(得分:0)
OR
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/rl">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#E74E4C"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="71dp" />
</RelativeLayout>
<ImageView
android:id="@+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@id/rl"
android:layout_margin="16dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@id/imagetwo"
android:layout_margin="16dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</ScrollView>