我创建了一些自定义元素,我想以编程方式将它们放置在右上角(n顶部边缘的m像素和右边缘的像素).因此我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
Run Code Online (Sandbox Code Playgroud)
如何获得screenWidth并screenHeight在主要活动?
我有一个显示相机预览的活动,因此需要将其设置为仅景观.在底部(无论设备旋转)我想显示文本视图.我正在使用OrientationEventListener,它从自然位置给出设备的方向.我可以实现一个在纵向默认设备上运行良好的解决方案,但是为了使它在横向默认设备上工作,我需要知道在这样的设备上运行.那么问题是如何检查呢?
在布局资源XML中,我有3个RelativeLayout(s),它们位于主RelativeLayout中.视图将垂直显示.这三个RelativeLayout()彼此相邻设置,我想让它们填满整个屏幕,无论屏幕大小是多少.我的布局视图:
<RelativeLayout 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"
android:background="@drawable/backg"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/top_mr_image"
android:src="@drawable/temp" />
<RelativeLayout
android:id="@+id/r1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="10dp"
android:background="@drawable/r1bg"
android:layout_weight="1"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/txt_mr_right"
android:layout_marginRight="@dimen/txt_mr_right"
android:layout_marginTop="39dp"
android:text="S"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/textView1"
android:layout_marginLeft="@dimen/txt_mr_right"
android:layout_marginRight="@dimen/txt_mr_right"
android:text="T"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/r2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/r1"
android:layout_toRightOf="@+id/r1"
android:layout_weight="1" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/r3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/r2"
android:layout_toRightOf="@+id/r2"
android:layout_weight="1" >
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我设置weight=1并layout_width=0dp为每个relativeLayout和这个技术使用按钮,我认为相同的将是relativeLayout,似乎我的想法是错误的.任何的想法?
UPD1:我添加了一张我想要的图像

的src应拉伸其宽度match_parent,同时保持高宽比.当图像大于父图像时,它会正确缩小.但是当图像较小时,它不会向上扩展.(插图显示了所需的行为).

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/foo"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
使用ScaleType.fitXY伸展width唯一
嗨我在LinearLayout中使用以下布局结构
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textColor="#000" />
<TextView
android:id="@+id/tv2"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:paddingRight="10dp"
android:textColor="#000" />
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px"
android:layout_height="320px"
android:gravity="center" >
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
并且想要从java文件中动态设置相对布局的宽度和高度,而不是在xml文件中将其设置为320px但不能这样做,因为我将限制仅限于纵向模式,因此方向更改不是问题.可以使用match_parent在全屏幕上设置相对布局,但我必须在屏幕上放置另一个视图,这样才有可能,或者我必须以另一种方式实现它...
我想知道如何测量视图的尺寸.就我而言,它是绝对布局.我已经阅读了有关这些问题的答案,但我仍然没有得到它.
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
drawOval();
}
public void drawOval(){ //, int screenWidth, int screenHeight){
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
int screenWidth = layoutbase.getWidth();
int screenHeight = layoutbase.getHeight();
Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);
Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);
...some code ...
((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));
mybutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showText(mybutton);
}
});
}
public void showText(View button){
int …Run Code Online (Sandbox Code Playgroud) 我正在使用 getRealMetrics() 方法并发现它已被弃用
val display = this.display
display?.getRealMetrics(outMetrics)
Run Code Online (Sandbox Code Playgroud)
任何人都知道替代方案是什么。
我有一个包含多个项目的回收站视图。我需要从底部开始显示项目,即第一个项目应该显示在底部,从下到上滚动后,将显示更多项目。为此,我尝试的是,
linearlayoutManager.setStackFromEnd(true);
Run Code Online (Sandbox Code Playgroud)
这有助于从底部开始项目。但是当有多个项目时,超过同时适合屏幕的大小,那么第一个项目会随着项目数量的增加而移动到顶部。
我也试过
linearlayoutManager.scrollToPosition(0);
Run Code Online (Sandbox Code Playgroud)
这没有改变任何东西。
这是一张图片,显示了我正在寻找的东西。
上面列表中还有一些项目仍然不可见。
我已经检查了图像中此视图的视图层次结构,该图像仅显示了一个 recyclerview 及其中的项目。并且recyclerview 的高度是match_parent,即顶部空间,没有项目显示recyclerview 下面的另一个视图,这是一个谷歌地图。我只需要知道如何在不使用更多额外视图的情况下实现 recyclerview 的此功能。
任何帮助将不胜感激..!!
我不确定这是否可行,或者如果我接近这个挑战是错误的.我正在制作2D平台游戏,所以当你到达屏幕边缘(宽度)时,会出现一个新的地图部分.目前,我为地图的每个部分制作了单个图像.问题是,在较大的设备上,这会留下大量的空白屏幕,而Image只适合特定的设备.
现在我想知道,我可以将我的ImageView(MAP)设置为负X值,然后当角色碰到边缘时,地图会向右移动(X会更高).
我正在使用XML(Java也很好),我没有CLUE如何处理这个问题.
我正在尝试选择ListActivity,类似于用于向启动器屏幕添加快捷方式的选项.我已经滚动了自己的页眉和页脚,我想在屏幕上的视图顶部和底部"粘".为了做到这一点,我使用RelativeLayout,标题设置为停靠到顶部,页脚设置为停靠到底部,列表设置为低于标题和页脚上方.就活动的整体布局而言,这正如我所期望的那样呈现.标题粘到顶部,页脚粘到底部,列表在它们之间滚动.
当我切换到RelativeLayout作为我的根时发生了一件奇怪的事情.请参阅以下屏幕截图:

我希望我的Activity的高度是wrap_content,所以表单只有它显示的内容一样高,但是一旦我切换到RelativeLayout,它似乎有效地呈现了Activity fill_parent,占用了整个屏幕,即使内容没有保证.请注意,没有足够的列表项来填充屏幕,这与fill_parent样式一起,在列表的末尾和页脚之间留下了一堆空格.我通过样式设置我的身高 - 这对于LinearLayout很好用,但现在似乎被忽略了.所以我尝试直接在RelativeLayout上对高度进行硬编码,但它仍然不起作用,仍然呈现为fill_parent.
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/GroupsList"
android:layout_height="wrap_content">
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/hdrGroups"
android:layout_alignParentTop="true">
<include layout="@layout/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</include>
</FrameLayout>
<FrameLayout style="@style/MyFooter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:id="@+id/ftrGroups">
<ImageView style="@style/CloseButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/add"
android:id="@+id/imgGroupsAdd"
android:clickable="true">
</ImageView>
</FrameLayout>
<ListView android:divider="#9f9f9f"
android:id="@+id/android:list"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/hdrGroups"
android:layout_above="@id/ftrGroups">
</ListView>
<TextView android:text="@string/browser_no_groups"
style="@style/ListedItemB"
android:id="@+id/android:empty"
android:layout_height="wrap_content"
android:layout_above="@id/ftrGroups"
android:layout_below="@id/hdrGroups"
android:layout_width="fill_parent">
</TextView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
所有布局都是通过XML完成的,......我没有在代码中做任何布局.我如何获得粘性页眉和页脚,同时让整个活动在wrap_content其高度模式下运行?还有其他方法我应该去做这个而不是RelativeLayout吗?
android listactivity sticky-footer android-layout android-relativelayout
我可以在Android应用程序中以编程方式找到屏幕的宽度吗?我需要绘制画布,但它的宽度应该几乎像屏幕,我无法在java零件程序中设置match_parent.
我想在一些Android设备中获取我的布局或视图(不是屏幕尺寸)宽度,所以我该怎么做?例如:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:id="@+id/layout_scrol_left"/>
Run Code Online (Sandbox Code Playgroud)
我有这个布局,我想在运行时获得布局宽度和高度尺寸(在倾角),我该怎么做?
我有与包括布局的活动AdView有ads:adSize="SMART_BANNER"。它位于屏幕底部,在custom下方WebView。现在,WebView具有基于可用空间(像素尺寸)呈现的内容。因此,在渲染WebView内容之前,我需要知道SMART_BANNER将要占据的大小(尤其是高度),以及因此需要保留的大小WebView。从这里它指出,智能旗帜的高度可以是中的任何一个32dp,50dp和90dp。
据我所知,我需要等待AdView决定格式之后才能呈现WebView。我怎样才能做到这一点?也许通过设置AdListener?然而,该方法的AdListener似乎只是让我来决定,例如当一个广告被加载中...我不希望有等待广告加载,我只是想等待广告的实际大小被分配。
我知道我可以通过将设置AdView为BANNER,并将高度固定为来避免所有这些情况50dp,并且确实可以正常工作。但是我更喜欢SMART_BANNER主要是因为高度可以缩小到32dp小手机屏幕上的横向模式,从而为实际内容留出更多空间。
android ×13
admob ×1
deprecated ×1
dimensions ×1
height ×1
java ×1
kotlin ×1
layout ×1
listactivity ×1
pixel ×1
scale ×1
screen ×1
scroll ×1
width ×1