自定义布局在左侧有空间
问题描述:
问题是我有一个带有配置文件图像的ImageView作为WhatsApp在其聊天布局中的后向箭头。 我希望这个后退箭头位于操作栏的最左侧,图像应该是后退箭头。就像whatsapp一样。自定义布局在左侧有空间
我的XML代码接在这里
<RelativeLayout
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="wrap_content"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
<ImageView
android:id="@+id/navigation_back_post_comment"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignBottom="@+id/post_author_layout_inflate"
android:layout_alignTop="@+id/post_author_layout_inflate"
android:layout_gravity="left|center"
android:gravity="center_vertical"
android:scaleType="center"
android:src="@drawable/ic_arrow_back_black_24dp" />
<LinearLayout
android:id="@+id/post_author_layout_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/navigation_back_post_comment"
android:layout_toRightOf="@+id/navigation_back_post_comment"
android:gravity="center_vertical"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="40dp"
android:layout_height="40dp"
app:cardCornerRadius="20dp">
<ImageView
android:id="@+id/post_author_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_action_account_circle_40" />
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/post_author"
style="@style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textColor="@color/cardview_light_background"
android:textStyle="bold"
tools:text="someauthor" />
<TextView
android:id="@+id/post_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textColor="@color/cardview_light_background"
android:textSize="@dimen/email_text_size"
android:textStyle="bold"
tools:text="[email protected]" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/star_layout_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/post_author_layout_inflate"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/post_author_layout_inflate"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/star_cmnt_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:background="?attr/selectableItemBackground"
android:src="@drawable/ic_create_white_24dp" />
<TextView
android:id="@+id/post_num_stars_cmnt_inflate"
style="@style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/cardview_light_background"
android:textStyle="bold"
tools:text="7" />
</LinearLayout>
</RelativeLayout>
答
尝试用这种替代后退按钮代码:
<ImageView
android:id="@+id/navigation_back_post_comment"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignBottom="@+id/post_author_layout_inflate"
android:layout_alignTop="@+id/post_author_layout_inflate"
android:layout_gravity="left|center"
android:gravity="center_vertical"
android:scaleType="center"
android:src="@drawable/ic_arrow_back_black_24dp" />
说明:
不应将layout_width设置为固定宽度,而应将其设置为wrap_content。如果这还不能满足你的要求,你可以设置的android:paddingLeft =“一些负值‘和android:paddingRight =’一些负值‘或Android:paddingStart =’一些负值”和android: paddingEnd =“一些负值”(用于从右到左的支持)。尝试尝试使用不同的值来找到最佳配置。
负值可以工作,但这不是一个错误的方式来做,因为在Android的其他设备上它将不同@HarisGusic –
@Uttam Meerwal如果您将填充设置为dp值,那么它将在不同的设备上相应地缩放。例如,如果您的图像是24dp,并且您设置了android:paddingLeft =“ - 8dp”,则在每台设备上都会显示图像的16dp。另一种解决方案是裁剪源图像。我认为你使用的是谷歌提供的原始背部图标,所以它有一些左右填充。您可以尝试编辑图像以删除填充。但请注意,您将不得不编辑不同密度的图像的所有不同版本。 –