2016-05-12 4 views
2

UIの他の要素とデフォルトのマテリアルボタンを整列するのに問題があります。実際に私はAndroidのソースコードを見ていると、ボタンの背景がクリックされたときに影を描くと、ボタンの上昇に対処できるようにするインセットが含まれていますので、他のUI要素とマテリアルボタンを整列する方法

<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetLeft="@dimen/abc_button_inset_horizontal_material" 
    android:insetTop="@dimen/abc_button_inset_vertical_material" 
    android:insetRight="@dimen/abc_button_inset_horizontal_material" 
    android:insetBottom="@dimen/abc_button_inset_vertical_material"> 
    <shape android:shape="rectangle"> 
     <corners android:radius="@dimen/abc_control_corner_material" /> 
     <solid android:color="@android:color/white" /> 
     <padding android:left="@dimen/abc_button_padding_horizontal_material" 
      android:top="@dimen/abc_button_padding_vertical_material" 
      android:right="@dimen/abc_button_padding_horizontal_material" 
      android:bottom="@dimen/abc_button_padding_vertical_material" /> 
    </shape> 
</inset> 

、私は非常に基本的なを持っています

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="16dp" 
    > 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="#123456" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="test button alignment" 
     /> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="#123456" 
     /> 
</LinearLayout> 

ご覧のとおり、ボタンの左端が他のビューの左端と揃っていません。

enter image description here

は、だから私の質問は、UIを持つように、デフォルトのAndroidのボタンで、箱から出して取り扱う影/標高うまく整列を失うことなく、これらのインセットを取り除くための方法があり、ありますか?

ありがとうございます!

+0

これは、[光学レイアウト境界](https://developer.android.com/about/versions/android-4.3.html#OpticalBounds)を使用可能にする必要がありますが、私はトラブルにこれを取得しなければなりませんでした材料ボタンを使って具体的に作業する –

答えて

1

を値に加算も関与エリア事が襲っています。ボタンはクリック可能な領域がグラフィックよりも大きい。

レイアウトには既にパディングがあるため、ボタンには影を描くスペースが簡単にあります。ボタンの背景から水平のインセットを削除するだけです。

一般的なケースはより複雑です。あなたはすべき:ボタンの背景から

  • 削除はめ込み、
  • は常に
  • 全体クリッカブルエリアでクリックをキャプチャするためにヒットRECTを拡張し、影のためのスペースを残して、あなたのウィジェットの周りにいくつかのパディング/マージンを追加することを忘れないでください。

第2のものは単純です、最後のポイントは、例えばgetHitRect()を使用して行うことができます。

public void getHitRect(@NonNull Rect outRect) { 
    if (touchMargin == null) { 
     super.getHitRect(outRect); 
     return; 
    } 
    outRect.set(getLeft() - touchMargin.left, getTop() - touchMargin.top, getRight() + touchMargin.right, getBottom() + touchMargin.bottom); 
} 

あなたの場合はかなり私が書いた何をどの(カーボンを使用して解決するためにも非常に簡単です。 - )カスタムヒットRECTを追加して、インセットを削除します。

<?xml version="1.0" encoding="utf-8"?> 
<carbon.widget.LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="16dp"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="#123456" /> 

    <carbon.widget.Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginTop="8dp" 
     android:text="test button alignment" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="#123456" /> 
</carbon.widget.LinearLayout> 

enter image description here

そして、ここではそれがでどのように見えるかですデバッグモードをオンにします。赤い線は拡張ヒット矩形を示す。緑の線はビュー境界です。

enter image description here

+0

あなたの明確な説明のために@ Zielonyありがとう。私はちょうどボタンの背景のアンドロイドファイルをコピーし、あなたが言及したように水平のインセットを削除して終了しました。ところで素晴らしいライブラリー:-) – euitam

+0

Carbonって何? – deviant

+0

これはMD互換ライブラリです。私はデザインサポートライブラリがそこに来るまでに長い時間を書きました。 Carbonは2つのことを行います:1.リップル、ダイナミックシャドウ、ベクタグラフィックス、丸いコーナー、多数のウィジェットなどをバックポートします。2.完全なSVGサポート、追加のレイアウト属性、カスタムフォントなど一般的に欠けているものを実装します。参照:https://github.com/ZieIony/Carbon – Zielony

0

次のXMLを確認してください、私はandroid:layout_marginLeft="-4dip"

<?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" 
android:orientation="vertical" 
android:padding="16dp"> 

<View 
    android:id="@+id/te" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="#123456" /> 

<Button 
    android:id="@+id/bu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/te" 
    android:text="test button alignment" 
    android:layout_marginLeft="-4dip" 
    /> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="#123456" 
    android:layout_below="@+id/bu" 
    /> 
</RelativeLayout> 
+0

私は、負のインセットをハードコードすることが最善の考えだとは思わない。それはある日を逆火するかもしれない。 – Zielony

+0

はいZielonyあなたの答えは最適なオプションです。素晴らしい仕事(炭素) – UclidesGil

関連する問題