标签: android-layout

如何使ConstraintLayout中的元素居中

ConstraintLayout在我的应用程序中使用来进行应用程序布局.我正在尝试创建一个屏幕,其中一个EditText并且Button应该位于中心,并且Button应该低于EditTextmarginTop仅16dp.

这是我的布局和屏幕截图,它现在看起来如何.

activity_authenticate_content.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="com.icici.iciciappathon.login.AuthenticationActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/client_id_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_client_id"
            android:inputType="textEmailAddress" />

    </android.support.design.widget.TextInputLayout>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/authenticate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/login_auth"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@id/client_id_input_layout"
        app:layout_constraintRight_toRightOf="@id/client_id_input_layout"
        app:layout_constraintTop_toTopOf="@id/client_id_input_layout" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

android android-layout android-constraintlayout

176
推荐指数
7
解决办法
14万
查看次数

Gridview有两列和自动调整大小的图像

我正在尝试用两列制作gridview.我的意思是每排并排两张照片,就像这张照片一样.

在此输入图像描述

但是我的照片之间有空格,因为它的大小不一样.这是我得到的.

在此输入图像描述

如您所见,第一张图片隐藏了显示联系人姓名和电话号码的图例.而其他图片未正确拉伸.

这是我的GridView xml文件.如您所见,columnWidth设置为200dp.我希望它是自动的,所以图片会自动调整每个屏幕尺寸的大小.

<?xml version="1.0" encoding="utf-8"?>
<GridView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridViewContacts"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="2"
    android:columnWidth="200dp"
    android:stretchMode="columnWidth"    
    android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)

这是项目xml文件,它代表每个项目本身.

<?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/imageViewContactIcon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/linearlayoutContactName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="#99000000"
        android:layout_alignBottom="@+id/imageViewContactIcon">

        <TextView
            android:id="@+id/textViewContactName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            android:textSize="15sp"
            android:text="Lorem Ipsum" />       

        <TextView
            android:id="@+id/textViewContactNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="5dp"
            android:focusable="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:textSize="10sp"
            android:text="123456789" />

    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

所以我想要的是每行显示两个图像,并且无论屏幕大小如何,图像都会自动调整大小.我在布局上做错了什么?

谢谢.

android android-layout android-gridview

175
推荐指数
2
解决办法
16万
查看次数

174
推荐指数
3
解决办法
11万
查看次数

styles.xml中的自定义属性

我创建了一个自定义小部件,我在layout.xml中声明它.我还在attr.xml中添加了一些自定义属性.但是,当尝试在styles.xml中的样式中声明这些属性时,它正在给我No resource found that matches the given name: attr 'custom:attribute'.

我已将xmlns:custom="http://schemas.android.com/apk/res/com.my.package"styles.xml中的所有标记放入,包括<?xml>,<resources><style>,但它仍然给我相同的错误,它无法找到我的自定义XML命名空间.

但是,我可以使用我的命名空间手动为layout.xml中的视图分配属性,因此命名空间没有任何问题.我的问题在于使styles.xml知道我的attr.xml.

android android-layout android-xml android-styles

174
推荐指数
5
解决办法
6万
查看次数

LinearLayout,RelativeLayout和AbsoluteLayout有什么区别?

我对LinearLayout,RelativeLayout和AbsoluteLayout之间的区别感到困惑.有人可以告诉我他们之间的确切差异吗?

android android-layout android-linearlayout android-relativelayout android-framelayout

173
推荐指数
3
解决办法
21万
查看次数

为什么不总是使用android:configChanges ="keyboardHidden | orientation"?

我想知道为什么不在 android:configChanges="keyboardHidden|orientation"每个(几乎每个;))活动中使用?

产品:

  • 无需担心您的活动被轮换
  • 它更快

不太好:

  • 如果它们取决于屏幕尺寸,需要更改布局(例如,布局有两列左右)

坏:

  • 没有灵活的方法在不同的方向上有不同的布局
  • 使用碎片时不太好

但如果我们不使用不同的布局,为什么不呢?

android android-layout

173
推荐指数
1
解决办法
9万
查看次数

嵌套的Recycler视图高度不会包装其内容

我有一个应用程序来管理书籍集(如播放列表).

我想显示一个带有垂直RecyclerView的集合列表,并在每行内部显示一个水平RecyclerView中的书籍列表.

当我将内部水平RecyclerView的layout_height设置为300dp时,它会正确显示,但是当我将其设置为wrap_content时,它不会显示任何内容. 我需要使用wrap_content,因为我希望能够以编程方式更改布局管理器以在垂直和水平显示之间切换.

在此输入图像描述

你知道我做错了什么吗?

我的片段布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white">

    <com.twibit.ui.view.CustomSwipeToRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/shelf_collection_listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="10dp"/>

        </LinearLayout>

    </com.twibit.ui.view.CustomSwipeToRefreshLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

集合元素布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFF">

        <!-- Simple Header -->

    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/empty_collection"
            android:id="@+id/empty_collection_tv"
            android:visibility="gone"
            android:gravity="center"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/collection_book_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/> <!-- android:layout_height="300dp" -->

    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

书目项目:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="180dp"
              android:layout_height="220dp"
              android:layout_gravity="center">

        <ImageView
            android:id="@+id/shelf_item_cover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:maxWidth="150dp"
            android:maxHeight="200dp"
            android:src="@drawable/placeholder"
            android:contentDescription="@string/cover"
            android:adjustViewBounds="true"
            android:background="@android:drawable/dialog_holo_light_frame"/> …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-support-library android-studio android-recyclerview

172
推荐指数
10
解决办法
10万
查看次数

使用RecyclerView + AppBarLayout

我正在使用新的CoordinatorLayout与AppBarLayout和CollapsingToolbarLayout.在AppBarLayout下面,我有一个带有内容列表的RecyclerView.

我已经验证了当我在列表中上下滚动时,滚动滚动在RecyclerView上工作.但是,我还希望AppBarLayout在扩展期间顺利滚动.

向上滚动以展开CollaspingToolbarLayout时,一旦将手指从屏幕上抬起,滚动会立即停止.如果向上快速向上滚动,有时CollapsingToolbarLayout也会重新折叠.与使用NestedScrollView时,RecyclerView的这种行为似乎有很大不同.

我试图在recyclerview上设置不同的滚动属性,但我无法弄清楚这一点.

这是一个显示一些滚动问题的视频. https://youtu.be/xMLKoJOsTAM

这是一个显示RecyclerView(CheeseDetailActivity)问题的示例. https://github.com/tylerjroach/cheesesquare

以下是使用Chris Banes的NestedScrollView的原始示例. https://github.com/chrisbanes/cheesesquare

android android-appcompat android-layout android-design-library

171
推荐指数
5
解决办法
5万
查看次数

Android当软键盘可见时,如何在全屏模式下调整布局

当软键盘处于活动状态并且我已成功实现它时,我已经研究了很多调整布局但是当我android:theme="@android:style/Theme.NoTitleBar.Fullscreen"在清单文件中的活动标签中使用它时问题就出现了.

为此,我使用android:windowSoftInputMode="adjustPan|adjustResize|stateHidden"了不同的选项,但没有运气.

之后,我以FullScreen编程方式实现并尝试了各种布局,FullScreen但都是徒劳的.

我引用了这些链接,并在此处查看了许多与此问题相关的帖子:

http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html

http://davidwparker.com/2011/08/30/android-how-to-float-a-row-above-keyboard/

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/masterContainerView"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff">

    <ScrollView android:id="@+id/parentScrollView"
        android:layout_width="fill_parent" android:layout_height="wrap_content">

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:orientation="vertical">

            <TextView android:id="@+id/setup_txt" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Setup - Step 1 of 3"
                android:textColor="@color/top_header_txt_color" android:textSize="20dp"
                android:padding="8dp" android:gravity="center_horizontal" />

            <TextView android:id="@+id/txt_header" android:layout_width="fill_parent"
                android:layout_height="40dp" android:text="AutoReply:"
                android:textColor="@color/top_header_txt_color" android:textSize="14dp"
                android:textStyle="bold" android:padding="10dp"
                android:layout_below="@+id/setup_txt" />

            <EditText android:id="@+id/edit_message"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:text="Some text here." android:textSize="16dp"
                android:textColor="@color/setting_editmsg_color" android:padding="10dp"
                android:minLines="5" android:maxLines="6" android:layout_below="@+id/txt_header"
                android:gravity="top" android:scrollbars="vertical"
                android:maxLength="132" />

            <ImageView android:id="@+id/image_bottom"
                android:layout_width="fill_parent" android:layout_height="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android android-widget android-layout android-softkeyboard

170
推荐指数
13
解决办法
13万
查看次数

从android中的上下文获取活动

这个让我难过.

我需要在自定义布局类中调用activity方法.这个问题是我不知道如何从布局中访问活动.

ProfileView

public class ProfileView extends LinearLayout
{
    TextView profileTitleTextView;
    ImageView profileScreenImageButton;
    boolean isEmpty;
    ProfileData data;
    String name;

    public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
    {
        super(context, attrs);
        ......
        ......
    }

    //Heres where things get complicated
    public void onClick(View v)
    {
        //Need to get the parent activity and call its method.
        ProfileActivity x = (ProfileActivity) context;
        x.activityMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

ProfileActivity

public class ProfileActivityActivity extends Activity
{
    //In here I am creating multiple ProfileViews and adding them …
Run Code Online (Sandbox Code Playgroud)

android view hierarchy android-layout android-activity

169
推荐指数
6
解决办法
29万
查看次数