whl*_*hlk 12 xml xslt layout android
我想做一个类似于这个的布局:
www.ImageBanana.net - layout.png http://www.imagebanana.com/img/9kmlhy66/thumb/layout.png
屏幕上有四个方形按钮 - 每个按钮使用屏幕的一半/屏幕高度(以较小者为准).与屏幕尺寸/分辨率无关.
我已经尝试通过使用a实现这一点,LinearLayout但按钮最终使用正确的宽度,但仍然具有背景的高度(不再是正方形).
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_width="fill_parent"
android:text="@string/sights"
android:id="@+id/ApplicationMainSight"
android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_width="fill_parent"
android:text="@string/sights"
android:id="@+id/ApplicationMainSight"
android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="@string/usergenerated"
android:id="@+id/ApplicationMainUserGenerated" />
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="@string/tours"
android:id="@+id/ApplicationMainTour"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:www.ImageBanana.net - layout2.png http://www.imagebanana.com/img/i2ni6g4/thumb/layout2.png
我怎样才能使布局看起来像上面的图像?
小智 25
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
// Desired width-to-height ratio - 1.0 for square
private final double mScale = 1.0;
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width > (int)((mScale * height) + 0.5)) {
width = (int)((mScale * height) + 0.5);
} else {
height = (int)((width / mScale) + 0.5);
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ConstraintLayout来实现此目的。您只需在 50% 屏幕宽度的位置添加垂直辅助线,将按钮与辅助线的左侧和右侧对齐,并将其调整为 1:1 的宽高比。一切都可以在布局 XML 文件中完成,无需额外的源代码。
\n\n一旦您完成了所有这些步骤,您将得到如下结果:
\n\n<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n\n <Button\n android:id="@+id/button1"\n android:layout_width="0dp"\n android:layout_height="0dp"\n android:background="@android:color/holo_orange_light"\n android:text="1"\n app:layout_constraintDimensionRatio="H,1:1"\n app:layout_constraintEnd_toStartOf="@+id/guideline"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent" />\n\n <Button\n android:id="@+id/button2"\n android:layout_width="0dp"\n android:layout_height="0dp"\n android:background="@android:color/holo_blue_light"\n android:text="2"\n app:layout_constraintDimensionRatio="W,1:1"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toEndOf="@+id/guideline"\n app:layout_constraintTop_toTopOf="parent" />\n\n <Button\n android:id="@+id/button3"\n android:layout_width="0dp"\n android:layout_height="0dp"\n android:background="@android:color/holo_green_dark"\n android:text="3"\n app:layout_constraintDimensionRatio="W,1:1"\n app:layout_constraintEnd_toStartOf="@+id/guideline"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@+id/button1" />\n\n <Button\n android:id="@+id/button4"\n android:layout_width="0dp"\n android:layout_height="0dp"\n android:background="@android:color/holo_purple"\n android:text="4"\n app:layout_constraintDimensionRatio="W,1:1"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toEndOf="@+id/guideline"\n app:layout_constraintTop_toBottomOf="@+id/button2" />\n\n <android.support.constraint.Guideline\n android:id="@+id/guideline"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:orientation="vertical"\n app:layout_constraintGuide_percent="0.5" />\n\n</android.support.constraint.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\n\n以下是此布局在最小和最大屏幕上的外观:
\n\n\n小智 5
我的解决方案类似于第一个,但是,我在onLayout方法中进行了大小调整.不知道哪一个更好.
为方便起见,我将我的解决方案包装在一个不错的Android库中,请参阅https://github.com/ronaldsteen/Android-SquareLayout-Library
| 归档时间: |
|
| 查看次数: |
20152 次 |
| 最近记录: |